Make 'def' and 'class' usable within expressions

Shane Hathaway shane at zope.com
Thu Mar 25 19:13:55 CET 2004


Glenn Andreas wrote:
> What do you do if the expression is in an "if" statement?
> 
> 
> if map(def(x,y,z),list1,list2,list3):
>    # what goes here?  The body of the function
>    # or the body of the if?  And how does the rest of it
>    # look?

Ah, that's an interesting concern.  The same applies to "for" and 
"while" statements.  This would have to be a syntax error.  Rewriting it 
isn't too difficult, though, if you're willing to create an extra variable.

     condition = map(def(x,y,z),list1,list2,list3):
         function body
     if condition:
         do stuff

> Also, limiting to "one per expression" prevents you from doing something 
> like:
> 
>    myHandlers = {
>       "click" : def(x,y):
>                      print "click at x,y"
>       "enter" : def():
>                      print "mouse entered"
>       "exit"  : def():
>                      print "mouse exited"
>    }

That example reads well, although it's odd that each function can have a 
different set of arguments.  I think you'd really want a "case" 
statement instead, but Guido doesn't seem to want that.

Shane




More information about the Python-list mailing list