[Python-3000] prototype OO?

Raymond Hettinger rhettinger at ewtllc.com
Thu May 11 20:43:22 CEST 2006


Jim Jewett wrote:

>There are languages (Self; I think even javascript) whose OO is based
>on Prototypes rather than classes.  If an object doesn't have the
>attribute/method, then check its prototype (and so on recursively).
>
>When I have to emulate this in python (or Java), I just try to shove
>all my logic into classmethods, and use at most a singleton instance.
>
>Proxies are certainly the perfect example of something where you would
>want a "just like X, except" object, and would prefer to be able to
>use instances as the prototype.
>
>I don't think prototype OO should replace the current form, but it
>might be worth adding, even in Python 2.X.  Do you have a proposed
>implementation, or even a proposed syntax?
>
>  
>

I've been thinking about and wanting Prototype OO support for a long 
time but think it should NOT be conflated with rest of the language.

Instead, there should be two nexus points:
1) a module defining Object() -- last year, there was a comp.lang.python 
posting with working PurePython code for Object();
2) a slight liberalization of the syntax for the def-keyword to allow 
definitions to be written directly into a targeted namespace.

Those two steps will make it easy to experiment with Prototype OO but 
not muck-up the rest of the language.   The module docs can show code 
demonstrating how to use the two together:

Account = Object.clone(balance=0)
def Account.deposit(self, v):
    self.balance += v
def Account.withdrawal(self, v):
    self.balance -= v
def Account.show(self):
    print self.balance

myAccount = Account.clone()
myAccount.deposit(10)
myAccount.show()


Boy = Object().clone(gender = 'male')
def Boy.sayGender(self):
    print self.gender
Girl = Goy.clone(gender = 'female')

m = Girl.clone(name=megan, age=3)
m.sayGender()



Raymond


"""Oh give me a clone
of my own flesh and bone
with its Y chromosome changed to an X
and when it is grown
then my own little close
will be of the opposite sex
""" -- sung to the tune of "where the buffalo roam"


More information about the Python-3000 mailing list