python eval in "library" file -


if i've got file, we'll call him test1.py contains:

code=''' class something(object):   def __init__(self):     print "blah blah blah, horrible idea" def run():   print "don't preach @ me pretentious fool" ''' eval(compile(code, '<string>', 'exec'))  

then below eval statement, in same test1.py file can of course stuff like:

x = something() run() 

but... if i've got file called test2.py , want able run run() or instantiate something there after firing import test1? i'm assuming there manipulation of locals() or globals() necessary googles failing me here.

no, not think need kind of locals() or globals() manipulation, can - import test1 , , instantiate something object -

import test1 x = test1.something() test1.run() 

example/demo -

my a.py has same code pasted test1.py , , can -

>>> import >>> x = a.something() blah blah blah, horrible idea >>> a.run() don't preach @ me pretentious fool 

also, should listen __init__() says, horrible idea.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -