class chainlist:
def ___init__(self):
self.chain = [6 ,7]
self.create_block(proof=1)
blockchain = chainlist()
print(chainlist.chain)
错误:
Traceback (most recent call last):
File "main.py", line 11, in <module>
print(chainlist.chain)
AttributeError: class chainlist has no attribute 'chain'
答案 0 :(得分:1)
您的代码有两个问题:
init
需要被双下划线包围。请改为尝试:
class chainlist:
def __init__(self):
self.chain = [6 ,7]
blockchain = chainlist()
print(blockchain.chain)
答案 1 :(得分:0)
使用print(blockchain.chain)
而不是chainlist.chain
。下面是一个如何访问类中不同变量的示例。
class chainlist:
myvar = [0, 1] # class variable shared by all instances
def __init__(self):
self.chain = [6 ,7] # instance variable unique to each instance
blockchain = chainlist()
print(blockchain.chain)
print(chainlist.myvar)
请阅读此'Class and Instance Variables'以明确了解。