DEV Community

Discussion on: Creating a blockchain in 60 lines of Python

Collapse
rochacbruno profile image
Bruno Rocha • Edited on

Great article :)

This is considered harmful because you are assigning a mutable object in a method definition, all instances of Block will be mutating the same data reference.

def __init__(self, timestamp=time(), data=[]):
Enter fullscreen mode Exit fullscreen mode

Consider changing to

def __init__(self, timestamp=time(), data=None):
     data = [] if data is None else data
Enter fullscreen mode Exit fullscreen mode

Also timestamp=time() will be evaluated in the time the class is defined, when the program is started, if this is a long running process then you are stuck with a certain time()

def __init__(self, timestamp=None, data=None):
     timestamp = timestamp or time()
     data = [] if data is None else data
Enter fullscreen mode Exit fullscreen mode
Collapse
imjoseangel profile image
Jose Angel Munoz Author • Edited on

Fully agree @rochacbruno

The idea of the code was keeping it exactly the same as the Javascript for learning purposes but your comment is a perfect helper for the post. ♥

I have just changed it as per your comments.

Thank you!