-1

Let's say for a moment that I have the following module in python:

class Foo(object):
    pass

def foofunc():
    pass

foofunc()

Will foofunc() (I mean the last statement) ever be executed if I choose to import the module instead of executing it directly?

NlightNFotis
  • 9,559
  • 5
  • 43
  • 66
  • 5
    Isn't trying this is faster than asking on SO? :) – reclosedev Jan 20 '13 at 18:56
  • @reclosedev Not really, because I am working on a project at the moment, and trying it involves seriously screwing up if something goes wrong. You will argue that I can always revert the changes, but the whole matter is psychological. Fear is very demotivating in this case. – NlightNFotis Jan 20 '13 at 18:58
  • 2
    I'm just sayning, that creating two files: ``foo.py`` with above code, and ``bar.py`` with ``import foo`` is faster than asking it. – reclosedev Jan 20 '13 at 19:00
  • @reclosedev you just made me feel bad about myself :-$ – NlightNFotis Jan 20 '13 at 19:01
  • I recommend that developers live by "Suck it and see." idioms.thefreedictionary.com/suck+it+and+see -- meaning, write small test cases and see what happens. – hughdbrown Mar 30 '13 at 02:14

1 Answers1

8

Yes, it will be executed on first import.

Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124