Would Anonymous Functions Help in Learning Programming/Python?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Sep 24 03:09:01 EDT 2007


On Sun, 23 Sep 2007 18:32:28 -0700, Kay Schluehr wrote:

> On 22 Sep., 02:14, Bruno Desthuilliers
> <bdesth.quelquech... at free.quelquepart.fr> wrote:
>> Kay Schluehr a crit :
>> (snip)
>>
>> > I checked out Io once and I disliked it. I expected Io's prototype OO
>> > being just a more flexible variant of class based OO but Io couples a
>> > prototype very closely to its offspring. When A produces B and A.f is
>> > modified after production of B also B.f is modified. A controls the
>> > state of B during the whole lifetime of B. I think parents shall not
>> > do this, not in real life and also not in programming language
>> > semantics.
>>
>> I may totally miss the point, but how is this different from:
>>
>> class A(object):
>>      def dothis(self):
>>          print "A.dothis(%s)" % self
>>
>> class B(A):
>>      pass
>>
>> b = B()
>>
>> b.dothis()
>>
>> def dothat(self):
>>      print "function dothat(%s)" % self
>>
>> A.dothis = dothat
>> b.dothis()
> 
> It's not a big deal because you do not use a class to propagate
> mutable state unless you are using a class attribute intentionally ( i
> guess you do not overuse it just to avoid object encapsulation and
> make everything global ). When using a prototype as in Io you need to
> protect the state of the child object yourself.

Just like in Python!  Here `things` are shared between all "children":

class A(object):
    things = list()

    def add(self, thing):
        self.things.append(thing)

This is what happens too when you use this Io snippet:

A := Object clone do(
    things := list()
    
    add := method(thing,
        self things append(thing)
    )
)

If you don't want `things` to be shared you write an `init` method, in
both Python and Io.  Python:

class B(object):
    def __init__(self):
        self.things = list()

    def add(self, thing):
        self.things.append(thing)

And Io:

B := Object clone do(
    init := method(
        self things := list()
        self
    )
    
    add := method(thing,
        self things append(thing)
    )
)

The `init` is called by the default `clone` method automatically just like
`__init__()` in Python.  It is really much like the class/instance
relationship in Python with just the line between class and instance
blurred.

> You can do this by overwriting the objects slots but then you end up
> writing your own object constructors and the templates accordingly, also
> named "classes" by some. Not having dedicated object constructors,
> member variable initializations and the presumed class definition
> boilerplate comes at the price of introducing them on your own.

The mechanism is already there in Io, no need to invent, just use it.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list