【问题标题】:Python3 issue with instantiating a class实例化类的 Python3 问题
【发布时间】:2019-10-28 18:44:01
【问题描述】:

我正在尝试运行一些示例代码,但实例化类 Blockchain 时出现问题。我最后一次工作 python 是 2.7,我对最后一个 if 语句没有任何问题。

我在没有导入区块链的情况下尝试过,但会产生语法错误。

谁能解释 3.8 的问题是什么?

代码:

    class Blockchain:
    def __init__(self):
        self.chain = []
        self.pending_transactions = []

    def new_block(self):
        # Create a new Block and adds it to the chain
        pass

    def new_transaction(self):
        # Adds a new transaction to the list of transacations
        pass

    @staticmethod
    def hash(block):
        # Hashes a Block
        pass

    @property
    def last_block(self):
        # Returns the last Block in the chain
        pass

    if __name__ == "__main__":
        import Blockchain
        blockchain = Blockchain()
        print(blockchain.chain)

错误: /Users/denisputnam/PycharmProjects/excersize2/venv/bin/python /Users/denisputnam/PycharmProjects/excersize2/blockchain.py 回溯(最近一次通话最后): 文件“/Users/denisputnam/PycharmProjects/excersize2/blockchain.py”,第 1 行,在 类区块链: 区块链中的文件“/Users/denisputnam/PycharmProjects/excersize2/blockchain.py”,第 25 行 导入区块链 ModuleNotFoundError:没有名为“区块链”的模块

Process finished with exit code 1

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    没关系。最后一个 if 有错误的缩进级别。 :(

    【讨论】:

      猜你喜欢
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 2023-02-02
      相关资源
      最近更新 更多