Python vs. Io

Brian Kelley bkelley at wi.mit.edu
Thu Jan 29 22:57:00 CET 2004


John Roth wrote:
> "Daniel Ehrenberg" <LittleDanEhren at yahoo.com> wrote in message
> news:711c7390.0401291301.3f95794 at posting.google.com...
> 
> 
>>Can anyone show me how to so much as alias "for" to "each" in Python
> 
> 
> Without getting into the language wars, I think what you're looking
> for is called the Visitor Pattern (Design Patterns: GOF). It's actually
> trivial to implement as long as you're not looking for built-in support.

I think Daniel's example is a little bit more complicated than that.  It 
resembles more closly the lisp-ish macros that were discussed to death a 
while ago.

Note that in his code:
each((key, value) in map(x=1, y=2, z=3),
     write(key, ": ", value, "\n")
)

key and value are variable names that get used inside the function call.

Here is my pythonic(?) version, however it requries a lambda to bind the 
variable names.

def map(**kw):
     return kw.items()

def each(seq, func):
     for v in seq:
         apply(func, v)

from sys import stdout
write = stdout.write

each(map(x=1,y=2,z=3),
      lambda key, value: write("%s: %s\n"%(key, value)))

Brian




More information about the Python-list mailing list