寻找如何通过一个方法/函数在同一个类中的另一个方法/函数可访问的类中创建一个变量集,而不必在外面做多余的(和有问题的代码).
这是一个不起作用的例子,但可能会告诉你我正在尝试做什么:
#I just coppied this one to have an init method
class TestClass(object):
def current(self, test):
"""Just a method to get a value"""
print(test)
pass
def next_one(self):
"""Trying to get a value from the 'current' method"""
new_val = self.current_player.test
print(new_val)
pass
Run Code Online (Sandbox Code Playgroud)
您可以在一个方法中设置它,然后在另一个方法中查找它:
class TestClass(object):
def current(self, test):
"""Just a method to get a value"""
self.test = test
print(test)
def next_one(self):
"""Trying to get a value from the 'current' method"""
new_val = self.test
print(new_val)
Run Code Online (Sandbox Code Playgroud)
作为注释,您需要self.test在尝试检索之前进行设置.否则,它将导致错误.我通常这样做__init__:
class TestClass(object):
def __init__(self):
self.test = None
def current(self, test):
"""Just a method to get a value"""
self.test = test
print(test)
def next_one(self):
"""Trying to get a value from the 'current' method"""
new_val = self.test
print(new_val)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6556 次 |
| 最近记录: |