Python is a Dynamically Typed Language. Which means you can set the value of any variable without having to declare that variable.
In your code snippet, x = Node(50)
is creating a new node class object with x.left
assigned to 50.
Now by doing x.a = Node(40)
, we are just defining a new attribute of the object x
with name a
, which will be a Node class object with left
value of 50.
If you do dir(x)
it will give you something like this,
['__class__',
'__delattr__',
#Some other attributes
'__weakref__',
'a',
'data',
'left',
'right']
If you take a look at the last 4 attributes you can see that a
is now also added as an attribute of the x object.
Conclution
if you do x.new_attribute = Node(10,20,30)
when new_attribute
isn't present in the object then python will just create a new attribute inside the x
object with the name new_attribute
& assign it the value of Node(10,20,30)
.