python 我如何在创建创世块时修复这个错误

w8ntj3qf  于 2023-01-19  发布在  Python
关注(0)|答案(2)|浏览(112)
Traceback (most recent call last):
  File "C:\Users\RAC\crypto\...\blockchain.py", line 178, in <module>
    blockchain = Blockchain()
                 ^^^^^^^^^^^^
  File "C:\Users\RAC\crypto\...\blockchain.py", line 49, in __init__       
    self.chain = [self.create_genesis_block(0)]
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Blockchain.create_genesis_block() takes 1 positional argument but 2 were given

代码是这样的

class Blockchain:
    def __init__(self):
        self.chain = [self.create_genesis_block(0)]
        self.difficulty = 4
        self.nodes = dict()
        self.replicated_nodes = dict()
        self.coin_ledger = dict()

    def create_genesis_block(self):
        return Block("Genesis Block", "0", coin)

我试着添加其他参数,但由于我是新来的,我自己还没能正确地弄清楚

n8ghc7c1

n8ghc7c11#

def create_genesis_block(self):不带参数,可能您的意思是:

def create_genesis_block(self, block_num):
        return Block("Genesis Block", block_num, coin)

@staticmethod 
def create_genesis_block(block_num):
        return Block("Genesis Block", block_num, coin)
wpx232ag

wpx232ag2#

使用self构造函数时,需要初始化类,请尝试

instanse = Blockchain()
instanse.create_genesis_block()

相关问题