在python中的一个类方法之间传输数据

时间:2014-10-07 09:18:07

标签: python class oop parameter-passing

我有一个班级

class worker:   
    def __init__(self, mainProject, victims):
        self.mainProject = mainProject
        self.victims = victims
    def mainWork(self, mainProject, victims):
        ...
    def send(self):
        ...

mainWork()中我定义了一些变量,浏览器对象和其他数据,我需要将它们转移到 send()

2 个答案:

答案 0 :(得分:1)

只需在self中分配和阅读您想要的内容:

def mainWork(self, mainProject, victims):
    self.whatever = 42
def send(self):
    print self.whatever

答案 1 :(得分:1)

做这样的事情: -

class worker:   
    def __init__(self, mainProject, victims):
        self.mainProject = mainProject
        self.victims = victims
    def send(self):
        print self.mainProject, self.victims

def main():
    w = worker('programming', 'Jess')
    w.send()

if __name__ == '__main__':
    main()