Smalltalk blocks in Python
Martin von Loewis
loewis at informatik.hu-berlin.de
Fri Jan 28 14:56:12 EST 2000
Thierry Thelliez <thierry at acm.org> writes:
> I come from the Smalltalk world and I am trying to understand Python.
>
> Is there a Smalltalk blocks equivalent in Python ?
Not really. You can do a lot with local functions, though:
def while_do(cond,block):
while cond():
block()
class C:
def __init__(self,n):
self.counter = 0
self.values = []
def more_to_do(self=self,max=n):
return self.counter<max
def step(self=self):
self.counter=self.counter+1
self.values.append(self.counter)
while_do(more_to_do,step)
c = C(6)
print c.values
I'm not certain that this is a good example, but it shows how
Smalltalk-like things could be done. I hope it also shows that things
shouldn't be done the Smalltalk way all the time :-)
Anyway, you have functions as first-class objects and can pass them
around. The problem is that functions only have their local scope, not
the scope where they've been declared in. So to pass values to the
function, you'll have to bind some parameters with default values.
Regards,
Martin
More information about the Python-list
mailing list