Prothon Prototypes vs Python Classes

Glenn Andreas gandreas at no.reply
Thu Apr 1 09:47:36 EST 2004


In article <slrnc6mvgv.rga.joe at gate.notcharles.ca>,
 Joe Mason <joe at notcharles.ca> wrote:

> In article <69cbbef2.0403311324.32837704 at posting.google.com>, has wrote:
> > Joe Mason <joe at notcharles.ca> wrote in message 
> > news:<slrnc6e2sm.fhj.joe at gate.notcharles.ca>...
> >> In article <69cbbef2.0403280928.438d194f at posting.google.com>, has wrote:
> >> > # Library pseudocode
> >> > 
> >> > _fooRegistry = []
> >> > 
> >> > obj _Foo: # prototype object
> >> >     # Foo's code goes here
> >> > 
> >> > def Foo(): # constructor function
> >> >     newFoo = _Foo.copy()
> >> >     _fooRegistry.append(newFoo)
> >> >     return newFoo
> >> > 
> >> > 
> >> > Dirt simple with not an ounce of class metaprogramming in sight.
> >> 
> >> Is Foo() the standard syntax for making a new _Foo object?  If this is a
> >> new wrapper you just created, then it's no different than adding a
> >> register() method - the user has to know that something different is
> >> being done.
> >> 
> >> If Foo() is the standard syntax, and you're just overriding the
> >> implementation here, does that get inherited?  If so, this missed the
> >> condition that only some of the descendants should get added to the
> >> registry.  If not - well, inheriting the constructor seems like the more
> >> useful behaviour in general, so why not?
> > 
> > Don't think you've quite got the point of prototype-based OO yet.
> > There's no real 'standard syntax' for making objects; nor is there any
> > inheritance, classes, metaclasses, or instances. To create a new
> > object from scratch, execute the code that defines it. Alternatively,
> > duplicate an existing object.
> 
> There certainly is a standard syntax for making objects.  I need to know
> how to "duplicate an existing object".  Is it "obj.dup()"?
> "obj.clone()"?  "duplicate obj"?  Most probably, the language (or
> standard library) defines one of these, and I expect most objects to be
> duplicated in the same way.
> 
It can be a bit more subtle than that in a prototype based system, since 
there are two things that are different, but have nearly identical 
results, but could both be considered "duplicate an object".

In general, you don't want to _duplicate_ the object, but rather use 
that object as "prototype" for the new object.

First, you can make a new object that has all the same 
slots/properties/attributes as the first object - let's just call that 
"clone" for now (and we'll assume single inheritence, via a slot called 
"__parent")

   > print obj.__parent
   None
   > obj.a = 1
   > obj.foo = def ():
         print "a=",self.a
   > obj.foo()
   a=1
   > obj1 = obj.clone()
   > print obj1.__parent
   None
   > obj.a = 2 # change something in the original object
   > print obj1.a # unchanged in the "clone"
   1
   > obj1.foo()
   a=1

and we'll make the second object that has first object as the __parent 
(and we'll pretend that the syntax is "new <some expression>"

   > obj2 = new obj1
   > print obj2.__parent
   <obj1>
   > obj2.foo()
   a=2 # since it inherits from obj1

(obviously, the object doesn't actually retain the binded local variable 
name)

If we looked at these three objects in a debugger:

   obj = <Object 0x1> { __parent: None, a: 1, foo: <Method 0x2> }
   obj1 = <Object 0x3> { __parent: None, a: 2, foo: <Method 0x2> }
   obj2 = <Object 0x4> { __parent: <Object 0x1> }

If we hadn't changed obj1.a, both obj1 and obj2 would behave identically 
to obj (they all have the same properties, same behavior), even though 
internally, they are fairly different (obj1 is independant of obj, while 
obj2 is completely dependant on obj).  Since both "data" and "behavior" 
is inherited, this is different from traditional class/instance 
organization (where an instance shares its behavior (methods) with all 
other instances, but has private data).  This also makes the issue of 
"obj2.a = 5" important, since at that point obj2 would normally get it's 
own value of "a" (and otherwise would inherit the value of obj.a), and 
you'd want a clean way to alter obj.a from a method in obj2 
("obj2.__parent.a = 3" would work, this is where directed resends are 
used in Self).

In general, you want obj2 (since it inherits everything from obj, much 
like an instance "inherits" stuff from its superclass), but both are 
useful.



More information about the Python-list mailing list