how can I replace a execfile with __import__ in class to use self variables

W

Wensheng

I have a py program below:
--------------------------------
class someclass:
def __init__(self):
self.var = "hell, world"

def printitout(self):
execfile("printit.py")
#__import__("printit") #doesn't work

if __name__=="__main__":
c = someclass()
c.printitout()
-------------------------------------------

the file printit.py has only one line:
--------------
print self.var
--------------

executing someclass works, but I can't replace execfile with import.
what globals, locals should I use to make __import__() work in that
situation?

Thank you for any help.
 
W

Wensheng

I just realized I can pass the object itself:
like
p=__import__("printit")
p.pr(self)

in printit.py
 
J

Jeff Shannon

Wensheng said:
I just realized I can pass the object itself:
like
p=__import__("printit")
p.pr(self)

Leaving no reason not do do *this* part as

import printit
printit.pr(self)

rather than using the internal hook function to do exactly the
standard thing.
in printit.py


(Though frankly I don't see the advantage of having this tiny function
in a separate file to begin with...)

Jeff Shannon
Technician/Programmer
Credit International
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top