A critic of Guido's blog on Python's lambda
Carl Friedrich Bolz
cfbolz at gmx.de
Sun May 7 15:24:26 EDT 2006
Bill Atkins wrote:
[snip]
>>Here's how one of the cells examples might look in corrupted Python
>>(this is definitely not executable):
>>
>> class FallingRock:
>> def __init__(self, pos):
>> define_slot( 'velocity', lambda: self.accel * self.elapsed )
>> define_slot( 'pos', lambda: self.accel * (self.elapsed ** 2) / 2,
>> initial_position = cell_initial_value( 100 ) )
>> self.accel = -9.8
>>
>> rock = FallingRock(100)
>> print rock.accel, rock.velocity, rock.pos
>> # -9.8, 0, 100
>>
>> rock.elapsed = 1
>> print rock.accel, rock.velocity, rock.pos
>> # -9.8, -9.8, -9.8
>>
>> rock.elapsed = 8
>> print rock.accel, rock.velocity, rock.pos
>> # -9.8, -78.4, -627.2
>>
>>Make sense? The idea is to declare what a slot's value represents
>>(with code) and then to stop worrying about keeping different things
>>synchronized.
>>
>>Here's another of the examples, also translated into my horrific
>>rendition of Python (forgive me):
>>
>> class Menu:
>> def __init__(self):
>> define_slot( 'enabled',
>> lambda: focused_object( self ).__class__ == TextEntry and
>> focused_object( self ).selection )
>>
>>Now whenever the enabled slot is accessed, it will be calculated based
>>on what object has the focus. Again, it frees the programmer from
>>having to keep these different dependencies updated.
>>
>>--
>>This is a song that took me ten years to live and two years to write.
>> - Bob Dylan
>
>
> Oh dear, there were a few typos:
>
> class FallingRock:
> def __init__(self, pos):
> define_slot( 'velocity', lambda: self.accel * self.elapsed )
> define_slot( 'pos', lambda: self.accel * (self.elapsed ** 2) / 2,
> initial_value = cell_initial_value( 100 ) )
> self.accel = -9.8
>
> rock = FallingRock(100)
> print rock.accel, rock.velocity, rock.pos
> # -9.8, 0, 100
>
> rock.elapsed = 1
> print rock.accel, rock.velocity, rock.pos
> # -9.8, -9.8, 90.2
>
> rock.elapsed = 8
> print rock.accel, rock.velocity, rock.pos
> # -9.8, -78.4, -527.2
>
you mean something like this? (and yes, this is executable python):
class FallingRock(object):
def __init__(self, startpos):
self.startpos = startpos
self.elapsed = 0
self.accel = -9.8
velocity = property(lambda self: self.accel * self.elapsed)
pos = property(lambda self: self.startpos + self.accel *
(self.elapsed ** 2) / 2)
rock = FallingRock(100)
print rock.accel, rock.velocity, rock.pos
# -9.8, 0, 100
rock.elapsed = 1
print rock.accel, rock.velocity, rock.pos
# -9.8, -9.8, 95.1
rock.elapsed = 8
print rock.accel, rock.velocity, rock.pos
# -9.8, -78.4, -213.6
Cheers,
Carl Friedrich Bolz
More information about the Python-list
mailing list