looking for way to include many times some .py code from another python code

Brano Zarnovican zarnovican at pobox.sk
Tue Mar 8 07:47:26 EST 2005


On Tuesday 08 March 2005 12:41, Martin MOKREJŠ wrote:
> cat somefile.py
> a = 222
> b = 111
> c = 9
>
> cat anotherfile.py
>
> def a():
>     include somefile
>     postprocess(a)

What about :
def a():
  exec open('somefile.py')
  postprocess(a)

You can even use the optional dictionary parameter:

class klass(object):
  pass

a = klass()

exec open('somefile.py') in a.__dict__

print a.a, a.b, a.c
222 111 9

Cheers,

BranoZ




More information about the Python-list mailing list