473,378 Members | 1,393 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,378 software developers and data experts.

can a method access/set another's variables?

My code is:
-a.py-
import b

class A:
def __init__(self):
pass
def my_method(self):
var = 1
self.var = 2
b.set_var(self)
print var
print self.var

my_a = A()
my_a.my_method()

-b.py-
def set_var(self):
var = 2
self.var = 2

I want both var and self.var to be 2 at the end. Is there anything I
can pass to set_var() that will give it access to the variables in
my_method() like I can use self for the variables in the class A?
Thanks!

Apr 2 '07 #1
10 1165
asdf1234234 wrote:
-a.py-
import b

class A:
def __init__(self):
pass
def my_method(self):
var = 1
self.var = 2
b.set_var(self)
print var
print self.var

my_a = A()
my_a.my_method()

-b.py-
def set_var(self):
var = 2
self.var = 2

I want both var and self.var to be 2 at the end. Is there anything I
can pass to set_var() that will give it access to the variables in
my_method() like I can use self for the variables in the class A?
I hope there isn't a way to do this that simply. :) Why do you want to
do this, or is it idle curiosity? There is almost surely a better way to
solve your underlying problem.

You can *read* your caller's local variables (either pass locals() as an
argument or use inspect to get the frame locals), but writing to this
dictionary has undefined behavior.
--
Michael Hoffman
Apr 2 '07 #2
On Apr 1, 9:43 pm, Michael Hoffman <cam.ac...@mh391.invalidwrote:
asdf1234234 wrote:
-a.py-
import b
class A:
def __init__(self):
pass
def my_method(self):
var = 1
self.var = 2
b.set_var(self)
print var
print self.var
my_a = A()
my_a.my_method()
-b.py-
def set_var(self):
var = 2
self.var = 2
I want both var and self.var to be 2 at the end. Is there anything I
can pass to set_var() that will give it access to the variables in
my_method() like I can use self for the variables in the class A?

I hope there isn't a way to do this that simply. :) Why do you want to
do this, or is it idle curiosity? There is almost surely a better way to
solve your underlying problem.

You can *read* your caller's local variables (either pass locals() as an
argument or use inspect to get the frame locals), but writing to this
dictionary has undefined behavior.
--
Michael Hoffman
I am parsing a document which contains some lines with code I want to
eval or exec. However, due to the complexity of the parsing, I am
splitting it among different methods. So, if I eval something in one
method, it won't be there if I try to access its value a few lines
later since I happen to be in a different method in the parser. Thanks
for the help!

Apr 2 '07 #3
On Apr 1, 7:56 pm, "wswilson" <wswil...@gmail.comwrote:
On Apr 1, 9:43 pm, Michael Hoffman <cam.ac...@mh391.invalidwrote:
asdf1234234 wrote:
-a.py-
import b
class A:
def __init__(self):
pass
def my_method(self):
var = 1
self.var = 2
b.set_var(self)
print var
print self.var
my_a = A()
my_a.my_method()
-b.py-
def set_var(self):
var = 2
self.var = 2
I want both var and self.var to be 2 at the end. Is there anything I
can pass to set_var() that will give it access to the variables in
my_method() like I can use self for the variables in the class A?
I hope there isn't a way to do this that simply. :) Why do you want to
do this, or is it idle curiosity? There is almost surely a better way to
solve your underlying problem.
You can *read* your caller's local variables (either pass locals() as an
argument or use inspect to get the frame locals), but writing to this
dictionary has undefined behavior.
--
Michael Hoffman

I am parsing a document which contains some lines with code I want to
eval or exec. However, due to the complexity of the parsing, I am
splitting it among different methods. So, if I eval something in one
method, it won't be there if I try to access its value a few lines
later since I happen to be in a different method in the parser. Thanks
for the help!
class Parser(object):
def early_parse(self):
self.result1 = eval("10 + 5")

def later_parse(self):
self.result2 = self.result1 + eval("20 * 2")

p = Parser()
p.early_parse()
p.later_parse()
print p.result1
print p.result2

Apr 2 '07 #4
On Apr 1, 7:56 pm, "wswilson" <wswil...@gmail.comwrote:
On Apr 1, 9:43 pm, Michael Hoffman <cam.ac...@mh391.invalidwrote:
asdf1234234 wrote:
-a.py-
import b
class A:
def __init__(self):
pass
def my_method(self):
var = 1
self.var = 2
b.set_var(self)
print var
print self.var
my_a = A()
my_a.my_method()
-b.py-
def set_var(self):
var = 2
self.var = 2
I want both var and self.var to be 2 at the end. Is there anything I
can pass to set_var() that will give it access to the variables in
my_method() like I can use self for the variables in the class A?
I hope there isn't a way to do this that simply. :) Why do you want to
do this, or is it idle curiosity? There is almost surely a better way to
solve your underlying problem.
You can *read* your caller's local variables (either pass locals() as an
argument or use inspect to get the frame locals), but writing to this
dictionary has undefined behavior.
--
Michael Hoffman

I am parsing a document which contains some lines with code I want to
eval or exec. However, due to the complexity of the parsing, I am
splitting it among different methods. So, if I eval something in one
method, it won't be there if I try to access its value a few lines
later since I happen to be in a different method in the parser. Thanks
for the help!
class A(object):
def early_parse(self):
self.result1 = eval("10 + 5")

class MyParser(object):
def later_parse(self):
MyParser.a.result2 = MyParser.a.result1 + eval("20 *
2")

a = A()

p = MyParser()
p.a.early_parse()
p.later_parse()
print p.a.result1
print p.a.result2

Apr 2 '07 #5
On Apr 1, 7:43 pm, Michael Hoffman <cam.ac...@mh391.invalidwrote:
asdf1234234 wrote:
-a.py-
import b
class A:
def __init__(self):
pass
def my_method(self):
var = 1
self.var = 2
b.set_var(self)
print var
print self.var
my_a = A()
my_a.my_method()
-b.py-
def set_var(self):
var = 2
self.var = 2
I want both var and self.var to be 2 at the end. Is there anything I
can pass to set_var() that will give it access to the variables in
my_method() like I can use self for the variables in the class A?

I hope there isn't a way to do this that simply. :) Why do you want to
do this, or is it idle curiosity? There is almost surely a better way to
solve your underlying problem.

You can *read* your caller's local variables (either pass locals() as an
argument or use inspect to get the frame locals), but writing to this
dictionary has undefined behavior.
--
Michael Hoffman
class A(object):
def early_parse(self):
self.result1 = eval("10+5")
def later_parse(A_obj):
A_obj.result2 = eval("20*2")

a = A()
a.early_parse()
later_parse(a)
print a.result1
print a.result2

Apr 2 '07 #6
On Apr 1, 9:24 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
On Apr 1, 7:43 pm, Michael Hoffman <cam.ac...@mh391.invalidwrote:
asdf1234234 wrote:
-a.py-
import b
class A:
def __init__(self):
pass
def my_method(self):
var = 1
self.var = 2
b.set_var(self)
print var
print self.var
my_a = A()
my_a.my_method()
-b.py-
def set_var(self):
var = 2
self.var = 2
I want both var and self.var to be 2 at the end. Is there anything I
can pass to set_var() that will give it access to the variables in
my_method() like I can use self for the variables in the class A?
I hope there isn't a way to do this that simply. :) Why do you want to
do this, or is it idle curiosity? There is almost surely a better way to
solve your underlying problem.
You can *read* your caller's local variables (either pass locals() as an
argument or use inspect to get the frame locals), but writing to this
dictionary has undefined behavior.
--
Michael Hoffman

class A(object):
def early_parse(self):
self.result1 = eval("10+5")
def later_parse(A_obj):
A_obj.result2 = eval("20*2")

a = A()
a.early_parse()
later_parse(a)
print a.result1
print a.result2
Oops. That def should be:

def later_parse(A_obj):
A_obj.result2 = A_obj.result1 + eval("20*2")
Apr 2 '07 #7
On Apr 1, 7:56 pm, "wswilson" <wswil...@gmail.comwrote:
I am parsing a document which contains some lines with code I want to
eval or exec. However, due to the complexity of the parsing, I am
splitting it among different methods. So, if I eval something in one
method, it won't be there if I try to access its value a few lines
later since I happen to be in a different method in the parser. Thanks
for the help!
val = None
class A(object):
def early_parse(self):
global val
self.result1 = val = eval("10 + 5")
def later_parse():
global val
val += eval("20*2")

a = A()
a.early_parse()
later_parse()
a.result2 = val
print a.result1
print a.result2

Apr 2 '07 #8
asdf1234234 <ws******@gmail.comwrote:
My code is:
-a.py-
import b

class A:
def __init__(self):
pass
Incidentally, these last two lines are totally, utterly useless. Do NOT
define special methods like this -- just omit the whole def statement
and you'll have identical semantics, no wasted boilerplate, even better
performance.
Alex
Apr 2 '07 #9
On Apr 2, 1:04 am, a...@mac.com (Alex Martelli) wrote:
asdf1234234 <wswil...@gmail.comwrote:
My code is:
-a.py-
import b
class A:
def __init__(self):
pass

Incidentally, these last two lines are totally, utterly useless. Do NOT
define special methods like this -- just omit the whole def statement
and you'll have identical semantics, no wasted boilerplate, even better
performance.

Alex
I know they're useless. I was using the init method before and just
replaced what I had with pass instead of deleting it because I
expected to use it again later. Thanks for the tip, though.

Apr 2 '07 #10
On Apr 2, 11:01 am, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
On 1 Apr 2007 18:36:04 -0700, "asdf1234234" <wswil...@gmail.com>
declaimed the following in comp.lang.python:
My code is:
-a.py-
import b
class A:
def __init__(self):
pass

Delete this empty __init__() [and, consider converting the class to
new-style]
def my_method(self):
var = 1

var is a local to method only name, it goes away when the method
ends.
self.var = 2
b.set_var(self)
print var
print self.var
my_a = A()
my_a.my_method()
-b.py-
def set_var(self):
var = 2

Again, var is local to just set_var().
self.var = 2
I want both var and self.var to be 2 at the end. Is there anything I
can pass to set_var() that will give it access to the variables in
my_method() like I can use self for the variables in the class A?

The common scheme, unless you expect to have multiple instances of
this class with /separate/ local "var", would be to put var in a
"common" module

-=-=-=-=- common.py

var = None

-=-=-=-=-

Then have both a.py and b.py "import common", followed by changing
all "var =" to "common.var ="
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfr...@ix.netcom.com wulfr...@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-a...@bestiaria.com)
HTTP://www.bestiaria.com/
Thanks! I like the sound of that...it should work for what I need it
to do.

Apr 2 '07 #11

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

Similar topics

2
by: Bobby Howell | last post by:
Hi. I have the following which I use either on my server using cscript (.vbs file) or identical code in my ASP pages. The problem is when running in ASP I recieve the following error:...
6
by: Oliver | last post by:
Hi there! Does anyone know of a way on how you could access/loop through the variable names. E.g. if you have a struct and want to loop through all variables in that struct to give its value to...
12
by: ritchiebuckley | last post by:
I've been doing asp.net for so long now that I have forgotten about windows form programming so excuse this (seemingly) basic question. I have a form A which then opens up form B. I do stuff on...
5
by: Dan | last post by:
We have a simple site. It's a frameset with two frames a left and a right. The left frame is essentially a list of records from a database (using a server-side repeater control). When you click...
17
by: Woody Splawn | last post by:
I am finding that time after time I have instances where I need to access information in a variable that is public. At the same time, the books I read say that one should not use public variables...
10
by: Goran Djuranovic | last post by:
Hi all, Does anyone know how to declare a variable in a class to be accessible ONLY from a classes instantiated within that class? For example: ************* CODE ***************** Public...
2
by: raji20 | last post by:
This is my first page named test.php, In this page iam assigning the value to the session variable username,and I could able to access that value in this page. But when I access the same...
1
by: agendum97 | last post by:
On Jun 26, 4:53*pm, Gregor Kofler <use...@gregorkofler.atwrote: If possible for your scenario, you could potentially use eval for this. For example: function MyClass(val) { var printIt =...
0
nathj
by: nathj | last post by:
Ok, let me expand a bit, hopefully that will make te question clearer. I am learning AspectJ by refactoring some existing code. The curret plan is to handle logging via Aspects and then move on to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.