Multiple inheritance, super() and changing signature

Marko Rauhamaa marko at pacujo.net
Thu Jun 2 18:20:55 EDT 2016


Random832 <random832 at fastmail.com>:
> But from a class-definition perspective, __init__ is the one and only
> thing that should be called a constructor.

Not arguing agaist that, but from the *user's* perspective, I see the
class itself is the constructor function:

   class C: pass
   c = C()

You could say that the class statement in Python little else than
syntactic sugar for creating an object constructor.

For example, instead of:

   import math
   class Point:
       def __init__(self, x, y):
           self.x = x
           self.y = y
       def rotate(self, angle):
           self.x, self.y = (
               self.x * math.cos(angle) - self.y * math.sin(angle),
               self.x * math.sin(angle) + self.y * math.cos(angle))

you could write:

   import math, types
   def Point(x, y):
       def rotate(angle):
           nonlocal x, y
           x, y = (
               x * math.cos(angle) - y * math.sin(angle),
               x * math.sin(angle) + y * math.cos(angle))
       return types.SimpleNamespace(rotate=rotate)

> The fact that many languages don't have any way to override object
> allocation, and therefore no analogue to __new__, also contributes to
> this conclusion.

I see no point in Python having __new__, either. In fact, I can see the
point in C++ but not in Python.


Marko



More information about the Python-list mailing list