30def create_new_block(previous_block, data):
31 index = previous_block.index + 1
32 timestamp = datetime.datetime.now()
33 hash = calculate_hash(index, previous_block.previous_hash, timestamp, data)34 return Block(index, previous_block.previous_hash, timestamp, data, hash)
35
36
4
5
6class Block:
7 def __init__(self, index, previous_hash, timestamp, data, hash): 8 self.index = index
9 self.previous_hash = previous_hash
10 self.timestamp = timestamp
37 previous_block = get_previous_block(blockchain)
38 index = previous_block.index + 1
39 timestamp = int(time.time())
40 hash = calculate_hash(index, previous_block.previous_hash, timestamp, data) 41 block = Block(index, previous_block.previous_hash, timestamp, data, hash)
42 blockchain.append(block)
43
6
7
8class Block:
9 def __init__(self, index, previous_hash, timestamp, data, hash): 10 self.index = index
11 self.previous_hash = previous_hash
12 self.timestamp = timestamp
Defining a local variable or function with the same name as a built-in object makes the built-in object unusable within the current scope and makes the code prone to bugs.
dict = {} # dict is a Python builtin!
for idx, student in enumerate(students):
dict[student] = idx
roll_number = {}
for idx, student in enumerate(students):
roll_number[student] = idx