conditional print statement ?
Paul McGuire
ptmcg at austin.rr.com
Fri Apr 27 11:32:21 EDT 2007
On Apr 27, 9:45 am, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
> Paul McGuire <p... at austin.rr.com> wrote:
> > The Enable/Disable decorators on the Python wiki (http://
> > wiki.python.org/moin/PythonDecoratorLibrary?highlight=%28decorator
> > %29#head-8298dbf9ac7325d9ef15e7130e676378bbbda572) help you do
> > something very similar, without having to replicate the function being
> > enabled/disabled.
>
> > @(disabled,enabled)[Print_Info]
> > def printOrNot(arg):
> > print arg
>
> Pardon me for asking, but isn't that a syntax error? Decorator syntax is:
>
> "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE
>
> and you don't have a dotted_name.
My bad. The wiki example assigns the appropriate decorator to another
name, and then uses that name, like this:
debugFlag = int(False)
state = (disabled,enabled)[debugFlag] # <-- proper way to do this
@state
def debugPrint(s):
print s
print "here comes some debug output"
debugPrint("xyzzy is the secret word")
print "that was it"
I think early in the decorator syntax discussions, there were some
proposals that decorators could be expressions, but I guess I forgot
which way that was decided. The example in this post does work (and
so does the one on the wiki) .
-- Paul
More information about the Python-list
mailing list