[英]How to access a Variable or list or dictionary inside a method of a class, outside the class?
class test:
def __init__(self):
print('inside the code')
def add(self,x,y):
a=x+y
def list_test(self):
ar=[]
ar.append(1)
ar.append(2)
def dict_test(self):
dict_t={1:2,2:3}
cl=test()
How do access a
, ar
and dict_t
outside the class?如何在课堂外访问
a
、 ar
和dict_t
? I want to print a
and ar
here.我想在这里打印
a
和ar
。 Thank You.谢谢你。
Like this:像这样:
class test:
def __init__(self):
self.ar = []
self.dict_t = {}
self.a = 0
print('inside the code')
def add(self, x, y):
self.a = x+y
def list_test(self):
self.ar.append(1)
self.ar.append(2)
def dict_test(self):
self.dict_t = {1: 2, 2: 3}
t = test()
t.list_test()
print(t.ar)
t.add()
print(t.a)
Note:笔记:
Define your variables in the initiate function.在启动函数中定义变量。 Then you can access them through:
然后您可以通过以下方式访问它们:
class_name.var_name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.