conditional print statement ?

Paul McGuire ptmcg at austin.rr.com
Fri Apr 27 10:11:50 EDT 2007


On Apr 26, 7:31 am, Dustan <DustanGro... at gmail.com> wrote:
> On Apr 26, 1:58 am, Antoon Pardon <apar... at forel.vub.ac.be> wrote:
>
>
>
>
>
> > On 2007-04-25, Stef Mientki <S.Mientki-nos... at mailbox.kun.nl> wrote:
>
> > > hello,
>
> > > As part of a procedure I've a number sequences like this:
>
> > ><Python>
> > >      if Print_Info: print Datafile.readline()
> > >      else:                Datafile.readline()
> > ></Python>
>
> > > Is there a more compressed way to write such a statement,
> > > especially I dislike the redundancy "Datafile.readline()".
>
> > > thanks,
> > > Stef Mientki
>
> > You could consider the following
>
> > def Print(arg):
> >     print arg
>
> > def Noop(arg):
> >     pass
>
> or (untested):
>
> if Print_Info:
>     def printOrNot(arg):
>         print arg
> else:
>     def printOrNot(arg):
>         pass
>
> printOrNot(Datafile.readline())
>
>
>
> > (Print if Print_Info else Noop) (Datafile.readline())
>
> > --
> > Antoon Pardon- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

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

-- Paul




More information about the Python-list mailing list