mixing statements into J2

Jeremy Bowers jerf at jerf.org
Fri Aug 27 13:05:37 EDT 2004


On Thu, 26 Aug 2004 10:36:08 -0700, Robert Brewer wrote: 
> using:
>     if test:
>         memoize
>     else:
>         synchronize
>     classmethod
> def foo(self, *args):
>     stuff(args)

Others have pointed out the illegality of this, I would point out there is
a legal way for those who may not realize it:

if test:
    myDec = memoize
else:
    myDec = syncronize

using:
    myDec
    classmethod
def foo(self, *args):
    stuff(*args)



I quite frequently do this to create modules that adapt to the environment
they are in... e.g., I don't require Zope support but if you have it I
will dynamically derive a class from Persistant, or object otherwise:

base = object
try:
    import ZODB
    base = ZODB.Persistant
except:
    # one of those rare cases where a bare except doesn't matter
    pass

class Whatever(base):
    pass

(I don't actually do this anywhere but I do the equivalent elsewhere in my
code.)

This is one of those things that keeps me from ever wanting to go back to
C++; you shouldn't make a habit out of this but when you need it there is
no substitute.




More information about the Python-list mailing list