Python调用testBlock类方法时即使使用self仍报NameError如何解决
问题分析与修复方案
核心错误原因
- 方法调用范围错误:
testBlock是Block类内部定义的成员方法,不能直接在全局作用域调用,这是你触发NameError的直接原因。C++中类的非静态成员方法也需要先实例化对象才能调用,Python逻辑一致。 - 拼写不匹配问题:代码中存在多处方法名拼写不一致:
__init__中调用了不存在的self.hash_blockhash()- 方法定义为
hash_black(),但调用时写的是blockHash.hash_block()
- 语法使用错误:
- hashlib的
hexdigest是可调用方法,必须加括号执行,否则返回的是方法对象而非哈希字符串 - 你给
prev_blockhash传的是hashlib.sha256实例对象,直接转字符串不会得到哈希值,需要先调用hexdigest()获取结果
- hashlib的
- 传参逻辑错误:给
testBlock加self后,你创建Block实例时取的是当前对象的属性值,而非传入的参数,完全不符合预期。
修复方案
这里提供两种修复路径,你可以按需选择:
方案1:将testBlock改为全局独立函数(最贴合你当前使用逻辑)
import hashlib class Block(): def __init__(self, height = 0, timestamp = "", tx = "", nonce = 0, prev_blockhash = "", curr_blockhash = ""): self.height = height self.timestamp = timestamp self.tx = tx self.nonce = nonce self.prev_blockhash = prev_blockhash self.curr_blockhash = self.hash_block() # 修正方法名 def hash_block(self): # 统一方法名 returning = hashlib.sha256() neoHeight = str(self.height) neoTimestamp = str(self.timestamp) neoTx = str(self.tx) neoNonce = str(self.nonce) neoPrev_blockhash = str(self.prev_blockhash) all_str = (neoHeight + neoTimestamp + neoTx + neoNonce + neoPrev_blockhash) all_bytes = all_str.encode() returning.update(all_bytes) hashed = returning.hexdigest() # 加括号执行方法 return hashed # 移到类外作为全局函数 def testBlock(height, timestamp, tx, nonce, prev_blockhash): blockHash = Block(height, timestamp, tx, nonce, prev_blockhash) blockHashed = blockHash.hash_block() print('Block Hashed:') print(blockHashed) return blockHashed height = 0 timestamp = '2021-02-25 11:59:59.134365' tx = 'Alice, Bob, 10' nonce = 0 prev_blockhash = hashlib.sha256() prev_blockhash.update(b'0') prev_blockhash = prev_blockhash.hexdigest() # 提前拿到哈希字符串 target_hash = "e2169844b4dbb8a35b15c5da2cd1cccbeb873da319bf5f3983b15b5fb76bafa7" # 现在可以正常调用,不会触发NameError result_hash = testBlock(height, timestamp, tx, nonce, prev_blockhash) assert result_hash # 如果你要验证和目标哈希一致可以用:assert result_hash == target_hash
方案2:将testBlock声明为类的静态方法(保留在类内的结构)
如果要保留testBlock在Block类内部,可以用@staticmethod装饰器声明为静态方法,不需要self参数:
import hashlib class Block(): def __init__(self, height = 0, timestamp = "", tx = "", nonce = 0, prev_blockhash = "", curr_blockhash = ""): self.height = height self.timestamp = timestamp self.tx = tx self.nonce = nonce self.prev_blockhash = prev_blockhash self.curr_blockhash = self.hash_block() def hash_block(self): returning = hashlib.sha256() neoHeight = str(self.height) neoTimestamp = str(self.timestamp) neoTx = str(self.tx) neoNonce = str(self.nonce) neoPrev_blockhash = str(self.prev_blockhash) all_str = (neoHeight + neoTimestamp + neoTx + neoNonce + neoPrev_blockhash) all_bytes = all_str.encode() returning.update(all_bytes) hashed = returning.hexdigest() return hashed @staticmethod def testBlock(height, timestamp, tx, nonce, prev_blockhash): blockHash = Block(height, timestamp, tx, nonce, prev_blockhash) blockHashed = blockHash.hash_block() print('Block Hashed:') print(blockHashed) return blockHashed height = 0 timestamp = '2021-02-25 11:59:59.134365' tx = 'Alice, Bob, 10' nonce = 0 prev_blockhash = hashlib.sha256() prev_blockhash.update(b'0') prev_blockhash = prev_blockhash.hexdigest() # 调用时需要加类名前缀 result_hash = Block.testBlock(height, timestamp, tx, nonce, prev_blockhash) assert result_hash
内容的提问来源于stack exchange,提问作者user16919308
相关产品推荐
相关产品推荐

