Prototype OO

Steve Holden steve at holdenweb.com
Mon Mar 31 06:18:35 EDT 2008


sam wrote:
> Steven D'Aprano napisał(a):
> 
>>> I can see that Python and Javascript inheritance model is almost the
>>> same. Both languages are dynamically typed. And it seems that using
>>> "classes" in Python makes some things more complicated then it is
>>> necessary (eg functions, methods and lambdas are differen beeing in
>>> Python concept).
>> Please explain why you think that functions are more complicated in 
>> Python because of the class model.
> 
> Sorry for a late reply (I was out of the office).
> 
> 
> 1. You have different syntax for named and unnamed (lambdas) functions. 
> Functions and methods are different things in Python even if they have same 
> syntax. But all these are still a pieces of code that you use repeatedly to make 
> some task.
> 
A knife and scissors are both used to cut things, but that doesn't mean 
they are the same.

> 2. Static function doesn't need to reference "self", and Python forces 
> programmer to put "self" explicitly. Then you have to do some "tricks" on 
> function to become static. Python is said "nothing is really private", but 
> interpreter does some "tricks" to make __id hidden for a class.
> 
Static functions don't even need to live in classes, and certainly don't 
need references to "self", which only makes sense in the context of 
acting on instances of classes.
> 
> Some opinions:
> 
> 1. In early days you could do OOP in C -- you just used additional parameter in 
> a function. Then C++ appeared to make life easier: "when you write 
> mystring.toupper(), then toupper function gets hidden argument called "this"". 
> Programs are written in a high level object oriented languages now. In these 
> languages you still use the same syntax mystring.toupper(), but now you don't 
> pass object to a function (as in C), but you act on that object. So in the body 
> of toupper() you can REFERENCE object "mystring". So why do I have to put that 
> "strange" argument "self"?
> 
> This is not only my opinion 
> (http://apache.ece.arizona.edu/~edatools/Python/Prototypes.htm). Without "self" 
> you would use same syntax for ordinary functions, methods, and lambdas.
> 
I think you are bundling quite different things together here. The 
difference in semantics between bound methods (where the instance 
reference is added as a first argument) and regular functions (where no 
additional argument is supplied) has nothing to do with the difference 
between function and lambda definition syntax.

> 
> 2. Something is private because you can't reference that from outside the scope. 
> The wrong way is to make object properties private by declaring them private or 
> to do hidden actions (__id). For example all local variables in function are 
> private, because you can't access them from outside that function. Why desn't 
> this work for objects?
> 
> Again this is not only my opinion -- 
> http://www.crockford.com/javascript/private.html.
> 
The desire to provide information hiding is fundamentally against the 
Python philosophy, which basically says that attribute values are 
exposed for all to see. This avoids the nonsense of having to provide 
setter and getter methods which Java imposes on the programmer.

You use the references you provide as a drunken man uses a lamp post - 
more for support than enlightenment. Python is not JavaScript, and never 
will be. JavaScript is a better language than it's often given credit 
for, but its object semantics are closer to Self than to Python.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list