Prothon is switching to the .NET platform

Peter Otten __peter__ at
Sat Aug 7 08:31:44 CEST 2004

John Roth wrote:

> "Christopher T King" <squirrel at WPI.EDU> wrote in message
> news:Pine.LNX.4.44.0408061533250.25906-100000 at
>> On Fri, 6 Aug 2004, John Roth wrote:
>> > I think it's possible to do prototypes within Python by
>> > overriding the __getattribute__() magic method. It still
>> > wouldn't be very pretty, but it should be able to do
>> > everything except override the magic methods (those
>> > seem to have to be in the class object for new style
>> > classes).
>> Hm, I'd never thought of this before; something along the lines of:
>>  class derive(object):
>>      def __init__(self,parent):
>>          self.parent = parent
>>      def __getattribute__(self,attr):
>>          return getattr(self.parent,attr)
>>  myderivedobject = derive(myprototypeobject)
>> would do the trick quite nicely I guess.  I have one application that
>> could benefit quite nicely from prototypes; I'll see if I can't work this
>> in.  Thanks for the enlightenment!
> What I was thinking of was more along the lines of:
> class ProtoBaseClass(object):
>     def __getattribute__(self, attr):
>         # call object.__getattribute__(self, __dict__)
>         # find requested attribute

Wouldn't that part be implied if you used __getattr__() instead of

>         # if not found, loop through back pointer chain
>         # if it's not a function, return the attribute
>         # if it is a function, wrap it in a method object and return it.

This means that an object sees all changes in its prototypes until
explicitly assigned an attribute. Is this intentional? (real question, I'm
not familiar with prototyped languages)
>     def clone(self):
>         # create new instance
>         # insert back pointer to this instance
>         return <new instance>
> Then you can create a new instance simply by calling self.clone().
> The only time you'd call the class itself is to get a new root
> instance, which might even be a singleton (making an easy
> way to get the root instance!).
> The place where this gets really ugly is inserting functions
> into the instances. They have to be created at the module
> level, and then inserted into the instance by a module level
> function (somewhat similar to the way classmethod,
> staticmethod and property work).

I haven't fully thought this through, but it might work along the lines of

class Stop(object): # always raise AttributeError

class ProtoBaseClass(object):
    parent = Stop()
    def __init__(self, parent=None):
        if parent is None:
            self.parent = self.parent
            self.parent = parent
    # __getattr__()            
    # clone()

and then to clone with new methods:

class Cloned(ProtoBaseClass):
    parent = someInstance
    def method(self):

c = Cloned() # parent should be someInstance, method() should work as
> I'd be quite interested in what you [Christopher] come up with.

Me too.


More information about the Python-list mailing list