Is there Python equivalent to Perl BEGIN{} block?

Tim Chase python.list at tim.thechases.com
Wed Mar 12 19:49:54 CET 2008


> The subject says pretty much all, 

Given what I understand about the BEGIN block[1], this is how 
Python works automatically:

   bash$ cat a.py
   print 'a1'
   import b
   print 'a2'

   bash$ cat b.py
   print 'b'

   bash$ python a.py
   a1
   b
   a2

However, the first import does win and Python caches 
module-loading, so take care:

   bash$ cat x.py
   print 'one'
   import a
   print 'two'
   import b
   print 'three'

   bash$ python x.py
   one
   a1
   B
   a2
   two
   three

-tkc

[1]
http://www.cs.cf.ac.uk/Dave/PERL/node133.html








More information about the Python-list mailing list