Jay O'Connor wrote:
----------------------
def test(var):
    print var


#main test(3) ----------------------

I want to be able to import this module so I can see "ah ha, this module defines a function called 'test'", but I don't want the code at the bottom executed during the import.

If you have source control over this file, you could write it with the more standard idiom:


def test(var):
    print var

if __name__ == "__main__":
    test(3)

Then when the module is imported, only the def statement gets executed, not the 'test(3)'. Of course, if you don't have source control over the file, you can't do this...


Also note that code that is not protected by an
if __name__ == "__main__":
test may be part of the module definition, so examining a module without executing this code may be misleading, e.g.:


def test(var):
    print var

globals()['test'] = type('C', (object,), dict(
    f=lambda self, var, test=test: test(var)))

if __name__ == "__main__":
    test().f(3)

The module above turns 'test' from a function into a class, so if all you do is look at the def statements, you may misinterpret the module.

Steve
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to