mixing statements into J2

R

Robert Brewer

Can we insert conditional expressions in the
decorator list ?
allowed in the future.

"In the future" means "post-2.4, when we have an idea of what people
are doing with it". Right now, there's not a whole lot of use cases
for more complex expressions in the decorator area, and there's more
potential for horror. Guido made this call on a gut feeling, not on
any technical grounds. His gut is usually good on this.


I don't see why.

It's not a requirement, but it seems reasonable to me. If you're going
to set off a decorator suite and then mix in normal Python statements,
the two models collide over order of operation:

using:
if test:
memoize
else:
synchronize
classmethod
def foo(self, *args):
stuff(args)

The statements get evaluated top-to-bottom, but the decorators get
applied bottom-to-top. It would be one more confusing issue in an
already-confusing syntax. Not to mention that the current patch pushes
decorators onto the stack at compile-time, and then pops them off after
the function def. Mixing statements into the suite would move decorators
from compile-time to runtime (at least, I don't see any way to avoid
that without introducing a whole new layer of pragmas).

If I felt it were possible, I would have recommended that application
order in J2 be the opposite of A1. But I figured that would cause
another two weeks of debate, which we don't have. I certainly see that
"using:" provides a case for top-to-bottom, where "@decorator" implies
the opposite.
God no. Please don't. Work with the syntax that's chosen, then we can
revisit this for 2.5.

Waiting until then guarantees the question won't be addressed, due to
backward-compatibility issues which we will have then which we do not
have now. Which I'm willing to concede to be a foregone conclusion at
this point. Perhaps it would be enough just to say (to Guido), "this is
still an open issue (one of many)"?


Robert Brewer
MIS
Amor Ministries
(e-mail address removed)
 
J

Jeremy Bowers

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top