Re: Prothon Prototypes vs Python Classes

Discussion in 'Python' started by Joe Mason, Apr 1, 2004.

  1. Joe Mason

    Joe Mason Guest

    In article <>, has wrote:
    > Joe Mason <> wrote in message news:<>...
    >> In article <>, 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.

    If I want to duplicate an object and add it to a registry, I can
    obviously say "obj2 = obj.dup(); registery.append(obj2)". But if I were
    designing a library defining objects of this type which were *always*
    intended to be added the registry, it would be preferable for the
    standard "obj.dup()" syntax to do this automatically as well, so the
    user doesn't need to know about the extra registry step.

    Whether it's "the point" of prototype-based OO or not, such hiding of
    complexity is a valuable thing for the language to do. If it turns out
    that you just can't do this because it's not part of the philosophy,
    then the philosophy is lacking. (Unless you can give me an equivalent
    procedure which is covered by the philosophy, and demonstrate that it's
    just as good.)


    In the example you gave, the way to make this object was "Foo()".
    Because we're talking about several different languages here, which have
    syntax which are mild variations on each other, it can be confusing to
    tell what exactly a bit of posted code is referring to. That's why I
    asked (rhetorically) if "Foo()" was the standard way to create an
    object. I assume you're talking about Prothon, in which it *is*. In
    that case, my second question stands - how do you inherit the behaviour
    of adding to the registry, without rewriting the "_fooRegistry.append()"
    line for each cloned object?

    Joe
     
    Joe Mason, Apr 1, 2004
    #1
    1. Advertising

  2. In article <>,
    Joe Mason <> wrote:

    > In article <>, has wrote:
    > > Joe Mason <> wrote in message
    > > news:<>...
    > >> In article <>, 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.
     
    Glenn Andreas, Apr 1, 2004
    #2
    1. Advertising

  3. Joe Mason

    Joe Mason Guest

    In article <>, Glenn Andreas wrote:
    >> 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")


    <snip example>

    > 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>"


    <snip other example>

    This is certainly how Self does it. Other prototype based languages
    (such as Io) do not make this distinction. In Io, you use "clone" to
    make a new object whose parent is the object it was cloned from, and
    that's it.

    Joe
     
    Joe Mason, Apr 1, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael
    Replies:
    2
    Views:
    284
    Stephen Horne
    Apr 1, 2004
  2. Jacek Generowicz
    Replies:
    4
    Views:
    295
    Jacek Generowicz
    Apr 2, 2004
  3. Michele Simionato
    Replies:
    12
    Views:
    512
    Jonathan Gardner
    May 7, 2004
  4. Mark Hahn
    Replies:
    41
    Views:
    926
    Paul Boddie
    May 27, 2004
  5. Mark Hahn
    Replies:
    20
    Views:
    611
    Mark Hahn
    Jul 13, 2004
Loading...

Share This Page