explain __int__

obj3 = MyClass()

(it's the () that creates the object, not the =)

_ _new__ is the constructor: it creates the instance and returns it (overriding: advanced topic)

Along the way, it calls _ _init__ on the *already-created* instance, to
ask it to initialise itself ready for use. So, _ _init__ is an
"initialiser" for the instance, used often. Rarely you override _ _new__, but customizing _ _init__ is common: http://www.python.org/doc/ref/customization.html

obj = mytype.__new__(*args, **kwds)
if isinstance(obj, mytype):
mytype.__init__(obj, *args, **kwds)
return obj

Nevertheless, _ _init__ doesn't construct anything. You can even call
it to reinitialize an existing object.

Also, how can a constructor require 'self' as an argument...?
_ _init__(self, ...)

If the _ _init__ function is called by the constructor it cannot return a
value. However if called as a normal function, it can return a value.
_ _init__ is just a function that gets called during the construction


Page Information

  • 4 months ago [history]
  • View page source
  • You're not logged in
  • No tags yet learn more

Wiki Information


Update to PBwiki 2.0

An entirely new PBwiki experience, including folders and easier editing.

Convert Now for Free | Learn more