replace a method in class: how?

Bruno Desthuilliers onurb at xiludom.gro
Tue Jun 27 10:42:45 CEST 2006


Maric Michaud wrote:
> Le mardi 27 juin 2006 06:21, Bruno Desthuilliers a écrit :
> 
>>Maric Michaud a écrit :
>>(snip)
>>
>>
>>>In OOP Methods are defined in *classes* not in any arbitrary object
>>
>>Chapter and verse, please ? AFAIK, the first O in OOP stands for
>>"object", not for "class" !-)
>>
> 
> Hard to find it, indeed.
> 
> 
>>Classes are just an implementation convenience, and the fact that the
>>class-based model is the most usual one doesn't imply it's the only
>>valid one.
> 
> Maybe, never been told of another one, do you think of javascript prototypes ? 

There other prototype-based languages. And truth is that while having
classes, Python's object model is much more closer to javascript's one
than to Java or C++.

> Well, there are no methods in this model, only functions.

Nope. There are closures, not simple functions.

And FWIW, what do you think methods are in Python ? Yes, functions,
wrapped in a special descriptor.

> 
>>So there's no reason one shouldn't override (or add) a method 
>>on a per-object basis. As a matter of fact, it's perfectly legal (and
>>can be very convenient) to do so in Python.
> 
> I never saw the term "method" used for anything except for what it means for 
> classes,

Help on class instancemethod in module __builtin__:

class instancemethod(object)
 |  instancemethod(function, instance, class)
 |
 |  Create an instance method object.


> and moreover, the class model, even if it's just an implementation, 
> it's the one chosen by python.

There's a superficial similarity with "mainstream" static OOPLs like
Java/C++ etc. But Python's classes, and their relation to instances, are
as dynamic as can be, and really very different from what's in most books.

> For the common understanding of the model, IMHO, classes bind methods, 
> instances bind functions.

???

In Python, a class is nothing more than an object that:
1/ act as an object factory
2/ serves as delegatee for instances.

FWIW, the proper constructor (__new__, not __init__) is free to return
instances of any other class (as long as the initializer signature is
compatible).


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list