这段代码没有像我希望的那样运行:
class A():
def __init__(self,b,c):
self.b = 1
self.c = 2
def get_dict(self):
mydict={}
for att in A():
mydict[att]=self.att
return mydict
print(get_dict(s))
你的意思是:
class A():
def __init__(self):
self.b = 1
self.c = 2
def get_dict(self):
mydict={}
for att in [i for i in dir(A()) if not i.startswith('__')]:
mydict[att]=getattr(self,att)
return mydict
print(get_dict(A()))
输出:
{'b': 1, 'c': 2}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句