473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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

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.

Jul 18 '05 #1
2 1782
I just realized I can pass the object itself:
like
p=__import__("printit")
p.pr(self)

in printit.py
-------------
def pr(self):
print self.var
-------------------

Jul 18 '05 #2
Wensheng wrote:
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
-------------
def pr(self):
print self.var
-------------------

(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

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
1
by: bwobbones | last post by:
Hi, I'm having trouble making __import__ work with the two classes attached. The PrintHello() method can't be seen in the BMTest2 class - what am I doing wrong here? ...
3
by: Brano Zarnovican | last post by:
Hi ! I have a python script represented by a string. I need to execute it in a context. 'exec' does the job, but doesn't display the filename in tracebacks. 'execfile' is displaying the...
16
by: digitalorganics | last post by:
What's the difference between initializing class variables within the class definition directly versus initializing them within the class's __init__ method? Is there a reason, perhaps in certain...
0
by: Mitko Haralanov | last post by:
Hi all, I am going to do my best to describe the issue that I am having and hopefully someone can shed some light on it: I have three modules that a comprising the problem: ../core.py...
2
by: Dave Westerman | last post by:
I've got a Jython script where I'm doing an execfile() to pull in a small script that contains a function, which I then call. execfile(app_applscript) optionList = createOptionList(applOptions)...
5
by: George Sakkis | last post by:
I maintain a few configuration files in Python syntax (mainly nested dicts of ints and strings) and use execfile() to read them back to Python. This has been working great; it combines the...
2
by: Igor Kaplan | last post by:
Hello python gurus. I got quite unusual problem and all my searches to find the answer on my own were not successful. Here is the scenario: I have the python program, let's call it script1.py,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.