[Tutor] __new__ and __init__

rail shafigulin rail.shafigulin at gmail.com
Wed Aug 1 16:28:53 CEST 2012


Hello everyone.

I'm trying to understand how to use the two methods. I know that __new__ is
used to create an object, while __init__ to initialize. But I'm not sure
what happens when I create an object.

I found the following code in the book (Python 3 Object Oriented
Programming by Dusty Phillips)

import weakref

class CarModel:
  _models = weakref.WeakValueDictionary()

  def __new__(cls, model_name, *args, **kwargs):
    model = cls._models.get(model_name)
    if not model:
      model = super().__new__(cls)
      cls._models[model_name] = model
    return model

  def __init__(self, model_name, air = False, tilt = False, cruise_control
= False, power_locks = False, alloy_wheels = False, usb_charger = False):
    if not hasattr(self, "initted"):
      self.model_name = model_name
      self.air = air
      self.tilt = tilt
      self.cruise_control = cruise_control
      self.power_locks = power_locks
      self.alloy_wheels = alloy_wheels
      self.usb_charger = usb_charger
      self.intted = True


dx = CarModel("Fit DX")
lx = CarModel("Fit LX", air = True, tilt = True, cruise_control = True,
power_locks = True, alloy_wheels = True, usb_charger = True)

I assume when I call CarModel(<parameters>) __new__is being called first
and then __init__ after it.
1) Does it mean that __new__ and __init__ must have the same parameters? In
this particular case __new__ and __init__ both have model_name and if I
understand correctly when __new__ is called the rest of the parameters
(air, tilt, cruise_control, etc) are absorbed by the *args argument. Please
correct me if I am wrong.
2) What happens if I don't use the same parameters, say in the case of
__init__ I will remove model_name, will I still be able to call dx =
CarModel("Fix DX")

I realize questions might seem a bit strange or simplistic but I just want
to make sure I have a correct understanding of things.

Any help is appreciated.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120801/9d801454/attachment.html>


More information about the Tutor mailing list