Prothon is switching to the .NET platform
newsgroups at jhrothjr.com
Fri Aug 6 22:47:04 CEST 2004
"Christopher T King" <squirrel at WPI.EDU> wrote in message
news:Pine.LNX.4.44.0408061533250.25906-100000 at ccc8.wpi.edu...
> 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:
def __getattribute__(self, attr):
# call object.__getattribute__(self, __dict__)
# find requested attribute
# 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.
# 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 know someone
said they did this sometime in 2000, for 1.5.2, but when I tried
to make their code work yesterday it failed right away. A code
inspection left me wondering how it could possibly have worked
in the first place.
I'd be quite interested in what you come up with.
More information about the Python-list