I am just learning Python and I come from a C background so please let me know if I have any confusion / mix up between both.
Assume I have the following class:
class Node(object):
def __init__(self, element):
self.element = element
self.left = self.right = None
@classmethod
def tree(cls, element, left, right):
node = cls(element)
node.left = left
node.right = right
return node
This is a class named Node
, that overloads the constructor, to be able to handle different arguments if needed.
What is the difference between defining self.element
in __init__
only (as shown above) as opposed to doing the following:
class Node(object):
element, left, right = None
def __init__(self, element):
self.element = element
self.left = self.right = None
Isn't self.element
in __init__
the same as the class's element
variable defined? Wouldn't that just overwrite element
from None
to the element
value passed into __init__
?