__init__ def() , class()

Hi All,

What changes would you make to the following code? Or is init always used in conjunction with class()?

def __init__(self, a = 1, b = 2):
    self.a = a
    self.b = b
    print(a)
    

__init__(self, a = 1, b = 2))
print(__init__(self, a = 1, b = 2))

Thanks.

In this case __init__ needs to be part of a class definition.

class Example:
  def __init__(self, a = 1, b = 2):
    self.a = a
    self.b = b
    print(a)
    

e = Example(a = 1, b = 2)
print(e.a)

Hey… yeah __init__ is a special method to be used as a constructor to create more instances inside a class. so definiely works in conjuction with classes. ummh why would you need to print a inside the method… I think that will be redundant