conditional print statement ?

S

Stef Mientki

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
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Stef said:
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()".

d=Datafile.readline()
if Print_info: print d

It's still two lines, but only has a single call to .readline().

HTH,
Martin
 
T

Terry Reedy

| if Print_Info: print Datafile.readline()
| else: Datafile.readline()

Since both branches discard the data read, I presume Martin's fix is what
you really want.

| Is there a more compressed way to write such a statement,
| especially I dislike the redundancy "Datafile.readline()".

But for future reference, if you really do need to call a method in
multiple places (or even just multiple times in a loop) you can condense
like so:

dread = Datafile.readline # followed by
....
dread() # as needed

Terry Jan Reedy
 
A

Antoon Pardon

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

(Print if Print_Info else Noop) (Datafile.readline())
 
S

stef

Antoon said:
You could consider the following

def Print(arg):
print arg

def Noop(arg):
pass

(Print if Print_Info else Noop) (Datafile.readline())
thank you all for your answers,
I'll play a little with the suggested solutions.

cheers,
Stef Mientki
 
D

Dustan

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())
 
S

stef

or (untested):

if Print_Info:
def printOrNot(arg):
print arg
else:
def printOrNot(arg):
pass

printOrNot(Datafile.readline())
thanks for the creative solution, and indeed it does work ;-)

cheers,
Stef Mientki
 
P

Paul McGuire

or (untested):

if Print_Info:
def printOrNot(arg):
print arg
else:
def printOrNot(arg):
pass

printOrNot(Datafile.readline())




- 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
 
D

Duncan Booth

Paul McGuire said:
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.
 
P

Paul McGuire

Paul McGuire said:
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
 

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

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top