Simple prototyping in Python

Dave Benjamin ramen at lackingtalent.com
Fri Apr 30 13:52:03 EDT 2004


In article <69cbbef2.0404300855.20dd436b at posting.google.com>, has wrote:
> Dave Benjamin <ramen at lackingtalent.com> wrote in message news:<slrnc938vm.1v0.ramen at lackingtalent.com>...
>> The recent conversation on prototype-based OOP and the Prothon project has
>> been interesting. I've been playing around with the idea for awhile now,
>> actually, since I do a lot of programming in JavaScript/ActionScript. I feel
>> like people are focusing too much on the "prototype chain", which to me is
>> mainly an attempt at reintroducing inheritance. 
> 
> It doesn't even do that; at least not in the sense that class-based OO
> defines it, where inheritance merely describes the initial state for
> every object created from a class.

But that is how it is used, anyway. See any textbook describing OO
programming in JavaScript. The caveats are also typically described, and
other hacks like the "__proto__" attribute (which is the object's prototype
object, not to be confused with the constructor's prototype object,
"prototype") are used to exercise more control (or confusion) over the matter.

> In a proto-OO system all objects are _completely_ anonymous*, having
> no 'class' and all being of the same type, 'object'. In this respect,
> they're just like strings, lists, dicts, or any other 'anonymous'
> type; a list is a list is a list, for example, regardless of how it is
> used.

Not necessarily. In JavaScript, you can still do "instanceof", for example.

>> JavaScript has a syntax
>> for this:
>> 
>> var o = {a: 5, b: 6}
> 
> Looks on the surface like AppleScript's record type, which is roughly
> analogous to C structs... and useless for anything more than
> struct-style usage. While I did read about JS programming in the
> dim-n-distant past - it was one of a number of languages I looked at
> when learning proto-OO on AS, which lacked any decent learning
> material on the topic - I've pretty much forgotten everything since.
> So can I ask: is the JS structure more flexible; e.g. can one add
> functions to it to operate like methods on the other data in the
> structure? Or does it have to provide a second structure for proto-OO
> use, as AS does?

Sure! JavaScript supports function literals that are true closures. In fact,
this feature can be used to support private data, which has been described
in some detail by Douglas Crockford, here:

http://www.crockford.com/javascript/private.html

The same technique can be accomplished by Python, though it'd be really nice
to have code blocks or function literals that accept statements.

-- 
.:[ dave benjamin: ramen/[sp00] -:- spoomusic.com -:- ramenfest.com ]:.
:  please talk to your son or daughter about parametric polymorphism. :



More information about the Python-list mailing list