[Edu-sig] Python for Fun
Kirby Urner
pdx4d@teleport.com
Fri, 25 May 2001 12:55:29 -0700
Just for fun: tweaking logic.py Connector class with 2.1 idioms:
def connect (self, inputs) :
if type(inputs) != type([]) : inputs = [inputs]
self.connects += inputs # <-- here
def set (self, value) :
if self.value == value : return # Ignore if no change
self.value = value
if self.activates : self.owner.evaluate()
if self.monitor :
print "Connector %s-%s set to %s" % \
(self.owner.name,self.name,self.value)
[con.set(value) for con in self.connects] # <-- and here
Could also go:
self.connects.extend(inputs) (is that 1.5 or later?)
I find it interesting that you can define lists simply for
their side-effects, with the list itself saved nowhere. Indeed,
you can stick [1,2,3] (no assignment to a variable) anywhere
a method and nothing happens -- __repr__ isn't triggered
except at the command line.
I did have
map(lambda con: con.set(value), self.connects)
which makes use of nested scopes (value in lambda is getting a
pointer from the surrounding method), but list comprehension
doesn't see this as a case of scope nesting i.e. variables
within the brackets are locally scoped already.
Seems to me that list comprehension was already providing a
way to work around some of little lambda's awkwardness, even
without the from __future__ import nested_scopes option.
Kirby