一个方法可以访问/设置另一个变量吗? [英] can a method access/set another's variables?

查看:89
本文介绍了一个方法可以访问/设置另一个变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是:

-a.py-

导入b


A类:

def __init __(个体经营):

传递

def my_method(个体经营):

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(自我):

var = 2

self.var = 2

我希望var和self.var都是2在末尾。有没有什么我可以传递给set_var(),它可以让它访问

my_method()中的变量,就好像我可以将self用于A类中的变量一样?

谢谢!

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!

推荐答案

asdf1234234写道:
asdf1234234 wrote:

-a.py-

导入b

A类:

def __init __(自我):

通过

def my_method(个体经营):

var = 1

self.var = 2

b.set_var(个体经营)

打印var

打印self.var

my_a = A()

my_a.my_method()


-b.py-

def set_var(个体经营):

var = 2

self.var = 2

我希望var和self.var最后都是2。有没有什么我可以传递给set_var(),它可以让它访问

my_method()中的变量,就好像我可以将self用于A类中的变量一样?
-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?



我希望没有办法简单地做到这一点。 :)你为什么要这样做?b $ b这样做,还是好奇好奇?几乎可以肯定有更好的方法来解决你的潜在问题。


你可以*读*你的来电者的局部变量(要么通过当地人( )作为

参数或者使用inspect来获取框架本地文件),但是写入这个

字典有不确定的行为。

-

Michael Hoffman

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


4月1日晚上9:43,Michael Hoffman< cam.ac ... @ mh391.invalidwrote:
On Apr 1, 9:43 pm, Michael Hoffman <cam.ac...@mh391.invalidwrote:

asdf1234234写道:
asdf1234234 wrote:

-a.py-

import b
-a.py-
import b


class A:

def __init __(self):

pass

def my_method(个体经营):

var = 1

self.var = 2

b.set_var(个体经营)

print var

print self.var
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()
my_a = A()
my_a.my_method()


-b.py-

def set_var(self):

var = 2

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


我希望var和self.var最后都是2。有没有什么我可以传递给set_var(),它可以让它访问

my_method()中的变量,就好像我可以将self用于A类中的变量一样?
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?



我希望没有办法简单地做到这一点。 :)你为什么要这样做?b $ b这样做,还是好奇好奇?几乎可以肯定有更好的方法来解决你的潜在问题。


你可以*读*你的来电者的局部变量(要么通过当地人( )作为

参数或者使用inspect来获取框架本地文件),但是写入这个

字典有不确定的行为。

-

Michael Hoffman


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



我正在解析一个文档,其中包含一些代码我想要的代码

eval或exec 。但是,由于解析的复杂性,我将

在不同的方法之间进行分割。所以,如果我用一个

方法来评估某些东西,那么如果我尝试访问它的价值几行就不会出现这种情况

之后因为我碰巧在解析器中的另一种方法。谢谢

的帮助!

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!


4月1日晚上7:56,wswilson < wswil ... @ gmail.comwrote:
On Apr 1, 7:56 pm, "wswilson" <wswil...@gmail.comwrote:

4月1日晚上9:43,Michael Hoffman< cam.ac ... @ mh391.invalidwrote :
On Apr 1, 9:43 pm, Michael Hoffman <cam.ac...@mh391.invalidwrote:

asdf1234234写道:
asdf1234234 wrote:

-a.py-

import b
-a.py-
import b


class A:

def __init __(self):

通过

def my_method(个体经营):

var = 1

self.var = 2
b.set_var(个体经营)

print var

print self.var
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()
my_a = A()
my_a.my_method()


-b.py-

def set_var(self):

var = 2

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


我想要var和self.var最后都是2。有没有什么我可以传递给set_var(),它可以让它访问

my_method()中的变量,就好像我可以将self用于A类中的变量一样?
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?


我希望没有办法简单地做到这一点。 :)你为什么要这样做?b $ b这样做,还是好奇好奇?几乎可以肯定更好的方法来解决你的潜在问题。
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.


您可以*读取*调用者的本地变量(将locals()作为

参数传递或使用inspect来获取框架locals),但写入这个

字典有不确定的行为。

-

Michael Hoffman
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



我正在解析一个文档,其中包含一些包含我想要的代码的行

eval或exec。但是,由于解析的复杂性,我将

在不同的方法之间进行分割。所以,如果我用一个

方法来评估某些东西,那么如果我尝试访问它的价值几行就不会出现这种情况

之后因为我碰巧在解析器中的另一种方法。谢谢

的帮助!


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

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


这篇关于一个方法可以访问/设置另一个变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆