Prototype OO

Bruno Desthuilliers bruno.42.desthuilliers at
Fri Mar 21 10:50:09 CET 2008

sam a écrit :
> Bruno Desthuilliers napisał(a):
>> Most of the arguments in favor of prototypes seems to come to, mainly:
>> 1/ it lets you customize behaviour on a per-object base
>> 2/ it removes the mental overhead of inheritance, classes etc
>> Point 1. is a non-problem in Python, since you can already add/replace 
>> methods on a per-objec basis (ok, with a couple restrictions wrt/ 
>> __magic__ methods and such).
>> Point 2. is not (IMHO) such a problem in Python, where inheritance is 
>> mainly an implementation detail (it's not needed for polymorphic 
>> dispatch to work) and class hierarchy tend to be very flat, when they 
>> even exist.
>> Mainly, Python's object system is actually quite close to javascript 
>> here. That is, you get more or less the same flexibility. And a couple 
>> of powerful features you don't have with javascript (like hooks in the 
>> attribute lookup mechanism), too.
> Thanks for precise opinion and spending your time.
> I think that when you use statically typed language, than you have to 
> define classes, because compiler has to know what is allowed to do with 
> instances of that class.
> But when you use dynamically typed language, then classes are redundant, 
> because object is looked at when it is referenced. Dynamically typed 
> language needs only objects hierarchy and you can add properties 
> (methods, values) to that object during program execution.

Dynamic typing does not necessarily imply that you can add properties of 
modify behaviour at runtime. Most dynamically typed languages let you do 
that, but that's still orthogonal. And FWIW, you could emulate this in 
static languages using a hashtable, functors, and a couple accessors (a 
la __getattr__/__setattr__).

> In dynamically typed language when you create object A that is inherited 
> from another object B,  than object A knows that B is his predecessor. So
> when you reference A.prop, then prop is looked in A first, then in B, 
> then in predecessors of B, and so on.

What you're describing here is the inheritance mechanism of Python. And 
could apply just as well to javascript prototype mechanism. A javascript 
object has a reference to it's prototype object - that you can 
customize, rebind etc -, a Python object has a reference to it's class 
object - that you can customise, rebind etc...

Don't be fooled by the term "class" itself - it's meaning is totally 
different in a language like Python. I may be plain wrong here but I 
suspect you don't have a serious knowledge of Python's object model.

More information about the Python-list mailing list