473,569 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("print it.py")
#__import__("pr intit") #doesn't work

if __name__=="__ma in__":
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 1787
I just realized I can pass the object itself:
like
p=__import__("p rintit")
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__("p rintit")
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
17360
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 print "x:", x f()
6
3181
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
1410
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? **************************** class one - BMTest - in BMTest.py: **************************** import wx
3
1669
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 filename but it can only exec a script in a filesystem. I have tried: - to give exec a filename, like:
16
2904
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 situations, to choose one over the other? Thank you.
0
1108
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 ../log.py ../resources/simple/__init__.py
2
2149
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) When I do this at the global level in my main script, it works just fine. However, when I move this code into a function in my main script, then...
5
9374
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 convenience of pickle with the readability of Python. So far each configuration is contained in a single standalone file; different configurations are...
2
1627
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, this program needs to execute another python script, let's call it script2.py. In script1.py I have the statement: execfile('script2.py')...
0
7698
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.