[英]how to access my variable from one method to another in same class?
I tried creating instance variable but it doesn't seem to be working. 我尝试创建实例变量,但似乎无法正常工作。 I want my data_list value in some other script with providing any argument.
我想要我的data_list值在其他脚本中提供任何参数。
class MqttData:
def on_connect(self,client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe(TOPIC)
# The callback for when a PUBLISH message is received from the server.
def on_message(self,client, userdata, msg):
global count
data=msg.payload.decode('ascii')
self.data_list=data.split('\n')
print(self.data_list)
#updatesheet.update_spreadsheet(data_list)
def justafunction(self):
return self.data_list
Add at the top of your class: 在课程顶部添加:
def __init__(self):
self.data_list = None # initialises to None at class instantiation
Then further down check whether it has been assigned before using it: 然后在使用前进一步检查是否已分配:
...
if self.data_list:
do_something()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.