python输出传递变量_Python:如何在类实例之间传递变量或获取调用方?

class foo():

def __init__(self)

self.var1 = 1

class bar():

def __init__(self):

print "foo var1"

f = foo()

b = bar()

In foo, I am doing something that produces "var1" being set to 1

In bar, I would like to access the contents of var1

How can I access var1 in the class instance f of foo from within the instance b of bar

Basically these classes are different wxframes. So for example in one window the user may be putting in input data, in the second window, it uses that input data to produce an output. In C++, I would have a pointer to the caller but I dont know how to access the caller in python.

解决方案

As a general way for different pages in wxPython to access and edit the same information consider creating an instance of info class in your MainFrame (or whatever you've called it) class and then passing that instance onto any other pages it creates. For example:

class info():

def __init__(self):

self.info1 = 1

self.info2 = 'time'

print 'initialised'

class MainFrame():

def __init__(self):

a=info()

print a.info1

b=page1(a)

c=page2(a)

print a.info1

class page1():

def __init__(self, information):

self.info=information

self.info.info1=3

class page2():

def __init__(self, information):

self.info=information

print self.info.info1

t=MainFrame()

Output is:

initialised

1

3

3

info is only initialised once proving there is only one instance but page1 has changed the info1 varible to 3 and page2 has registered that change.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值