Encapsulation, inheritance and polymorphism

Terry Reedy tjreedy at udel.edu
Thu Jul 19 08:11:23 CEST 2012


On 7/18/2012 10:40 AM, Lipska the Kat wrote:

> fact ... and I have never been forced to admit that I don't know what I
> wrote six months ago.

That is an explicit objective of Python's design.

> Python looks like an interesting language and I will certainly spend
> time getting to know it but at the moment it seems to me that calling it
> an Object Oriented language is just plain misleading.

I just call it object-based and let be done with it, as I have no 
interest in arguing 'Object Oriented'.

What perhaps *you* need to know, given your background, is that nearly 
all syntax and most builtin callables wrap special method calls that can 
be defined on user classes.

'a + b' is executed as something like

try:
   return a.__add__(b)  # or the C type slot equivalent
except: # not sure what is actually caught
   try:
     return b.__radd__(a)  # r(everse)add
   except: # ditto
     raise TypeError("unsupported operand type(s) for +: {} and 
{}".format(a, b))

[Syntax exceptions: =, and, or]

'len(x)' calls x.__len__ and checks that the return value can be 
interpreted as an integer (I believe that means that it is an int or has 
an __index__ method, so that it can be used as a sequence index) and 
that its value is >= 0.

-- 
Terry Jan Reedy






More information about the Python-list mailing list