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

mysql - FireDac error 314 - but DLLs are in program directory -

git - How to list all releases of public repository with GitHub API V3 -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -