can a method access/set another's variables?

A

asdf1234234

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!
 
M

Michael Hoffman

asdf1234234 said:
-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.
 
W

wswilson

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.

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!
 
7

7stud

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
 
7

7stud

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
 
7

7stud

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.

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
 
7

7stud

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")
 
7

7stud

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
 
A

Alex Martelli

asdf1234234 said:
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
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
W

wswilson

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.
 
W

wswilson

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/

Thanks! I like the sound of that...it should work for what I need it
to do.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top