Python equivalent to a C trick

D

Dan

Is there a python equivalent of this trick in C?

Logic_Test ? True_Result : False_Result

Example:
printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );
 
S

Sam Holden

Is there a python equivalent of this trick in C?

Logic_Test ? True_Result : False_Result

Example:
printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );

print "you have %i %s" % ( num_eggs, ("egg","eggs")[num_eggs>1] )

But I don't know if int(False)==0 and int(True)==1 are actually
guaranteed. And I wouldn't use it since it's ugly (IMHO).

You don't get the "only evaluate one" goodness of ?:
 
G

George Yoshida

Dan said:
Is there a python equivalent of this trick in C?

Logic_Test ? True_Result : False_Result

Logic_Test and True_Result or False_Result
would be a Python counterpart.
Example:
printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );

So the exmaple is converted to:
print "you have %d %s"%(num_eggs, (num_eggs > 1) and "eggs" or "egg")

But be warned that if True_Result is evaluated as False(e.g. True_Result
is an empty string), this doesn't work.

If you don't know what object is considered False in Python, check the
following document.

* 2.3.1 Truth Value Testing
http://docs.python.org/lib/truth.html
 
T

Thomas =?ISO-8859-1?Q?Kr=FCger?=

Dan said:
Is there a python equivalent of this trick in C?

Logic_Test ? True_Result : False_Result

Example:
printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );

Logic_Test and True_Result or False_Result

Example:
print (num_eggs > 1) and 'eggs" or "egg"

Thomas
 
E

Erik Max Francis

Thomas said:
Logic_Test and True_Result or False_Result

Example:
print (num_eggs > 1) and 'eggs" or "egg"

Be very careful that the "True_Result" in your formulation actually is
itself a Python true value, or this won't work:

print 'egg' + (numEggs == 1 and '' or 's')

will not work as expected.
 
P

Peter Hansen

Sam said:
But I don't know if int(False)==0 and int(True)==1 are actually
guaranteed. And I wouldn't use it since it's ugly (IMHO).

From the language ref at http://docs.python.org/ref/types.html#l2h-37

Booleans
These represent the truth values False and True. The two objects
representing the values False and True are the only Boolean objects.
The Boolean type is a subtype of plain integers, and Boolean values
behave like the values 0 and 1, respectively, in almost all contexts,
the exception being that when converted to a string, the strings
"False" or "True" are returned, respectively.

So yes, it's fully guaranteed.
 
P

Paul McGuire

Sam Holden said:
Is there a python equivalent of this trick in C?

Logic_Test ? True_Result : False_Result

Example:
printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );

print "you have %i %s" % ( num_eggs, ("egg","eggs")[num_eggs>1] )

I would choose "!=" instead of ">" as the comparison operator. I think the
accepted vernacular is:

you have -2 eggs
you have -1 eggs
you have 0 eggs
you have 1 egg
you have 2 eggs
you have 3 eggs
you have 0.5 eggs
....

-- Paul
 
J

Josef Meile

Example:
printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );

print "you have %i %s" % ( num_eggs, ("egg","eggs")[num_eggs>1] )


I would choose "!=" instead of ">" as the comparison operator. I think the
accepted vernacular is:

you have -2 eggs
you have -1 eggs
you have 0 eggs
you have 1 egg
you have 2 eggs
you have 3 eggs
you have 0.5 eggs
No offense intended, but the negative and float cases don't make any
sense for me on this context. You can't have -2 eggs or 0.5 eggs. The
last case won't occure as well because the output is being parsed to
int, so, you will get "you have 0 egg", which is again false. This error
happens with both solutions (with > and with !=).

Regards,
Josef
 
J

Josef Meile

Josef said:
Example:
printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );


print "you have %i %s" % ( num_eggs, ("egg","eggs")[num_eggs>1] )



I would choose "!=" instead of ">" as the comparison operator. I
think the
accepted vernacular is:

you have -2 eggs
you have -1 eggs
you have 0 eggs
you have 1 egg
you have 2 eggs
you have 3 eggs
you have 0.5 eggs

No offense intended, but the negative and float cases don't make any
sense for me on this context. You can't have -2 eggs or 0.5 eggs. The
last case won't occure as well because the output is being parsed to
int, so, you will get "you have 0 egg", which is again false. This error
happens with both solutions (with > and with !=).
Correction: The error happens only when using the ">" operator. With the
"!=" doesn't happen.
 
P

Paul McGuire

Josef Meile said:
Example:
printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );

print "you have %i %s" % ( num_eggs, ("egg","eggs")[num_eggs>1] )


I would choose "!=" instead of ">" as the comparison operator. I think the
accepted vernacular is:

you have -2 eggs
you have -1 eggs
you have 0 eggs
you have 1 egg
you have 2 eggs
you have 3 eggs
you have 0.5 eggs
No offense intended, but the negative and float cases don't make any
sense for me on this context. You can't have -2 eggs or 0.5 eggs. The
last case won't occure as well because the output is being parsed to
int, so, you will get "you have 0 egg", which is again false. This error
happens with both solutions (with > and with !=).

Regards,
Josef
No offense taken! :)

The OP was looking for a general solution to <condition> ? <if-true-action>
: <else-action>, then gave us the "you have n egg(s)" example.

In the general case, the number for comparison isn't always an integer
quantity such as eggs. It could be "dollar(s)"/"euro(s)"/"Swiss
franc(s)"/"yen" (hmm, I guess "yen" isn't a problem...), or "ton(s) of
salami", or "pound(s) of cement", or "degree(s) Celsius", or... Anyway,
even though eggs are usually counted from 1 to n in integer steps, other
quantities can easily be negative and/or continuous. Still the singular -
in English, anyway - is usually used *only* when the quantity is 1.
Fractional and zero amounts, even though less than 1, still most naturally
use the plural form.

You have 0.5 dollars
You have gained 1 pound
You increased temperature by 0 degrees
You have -2 dollars (that is, you owe 2 dollars)

My point (which I guess didn't come across too well) was that this is a
typical coding and testing error, in which only positive integer values > 0
are assumed, because we often mentally equate "plural" with "more than 1".
But whether you are working in an integer, real, positive-only, or all
numbers context, testing with n != 1 should determine whether singular noun
should be used.

-- Paul
 
A

Asun Friere

Erik Max Francis said:
Be very careful that the "True_Result" in your formulation actually is
itself a Python true value, or this won't work:

print 'egg' + (numEggs == 1 and '' or 's')

will not work as expected.

For which reason the form (test and [true_result] or
[false_result])[0] is sometimes advocated. This starts to look a
little too cluttered for my liking, so I tend to use it only when the
'true_result' is a variable and might be instantiated to a negative
value.
 
J

Josef Meile

Paul said:
No offense taken! :)

The OP was looking for a general solution to <condition> ? <if-true-action>
: <else-action>, then gave us the "you have n egg(s)" example.
So, I guess it is like the "foo" keyword.
In the general case, the number for comparison isn't always an integer
quantity such as eggs. It could be "dollar(s)"/"euro(s)"/"Swiss
franc(s)"/"yen" (hmm, I guess "yen" isn't a problem...), or "ton(s) of
salami", or "pound(s) of cement", or "degree(s) Celsius", or... Anyway,
even though eggs are usually counted from 1 to n in integer steps, other
quantities can easily be negative and/or continuous. Still the singular -
in English, anyway - is usually used *only* when the quantity is 1.
Fractional and zero amounts, even though less than 1, still most naturally
use the plural form.

You have 0.5 dollars
You have gained 1 pound
You increased temperature by 0 degrees
You have -2 dollars (that is, you owe 2 dollars)
Good point ;-)
My point (which I guess didn't come across too well) was that this is a
typical coding and testing error, in which only positive integer values > 0
are assumed, because we often mentally equate "plural" with "more than 1".
But whether you are working in an integer, real, positive-only, or all
numbers context, testing with n != 1 should determine whether singular noun
should be used.
I like your solution and I think it even considers when n=0, while the
other example fails and write "You have 0 egg"
 
D

Dan Sommers

You have 0.5 dollars
You have gained 1 pound
You increased temperature by 0 degrees
You have -2 dollars (that is, you owe 2 dollars)

Don't forget this one: You have -1 egg.

Regards,
Dan
 
M

Mark Bottjer

Dan said:
Is there a python equivalent of this trick in C?

Logic_Test ? True_Result : False_Result

There's something close:

Logic_Test and True_Result or False_Result

print 'you have %i egg%s' % (num_eggs, (num_eggs != 1) and 's' or '')

The main problem with this is that False_Result will get executed if
Logic_Test is false *or* True_Result evaluates to false. Witness the
following broken variation of the above:

print 'you have %i egg%s' % (num_eggs, (num_eggs == 1) and '' or 's')

So it's not perfect, but it is workable. And as a previous post
indicated, this situation is not likely to change. :)

-- Mark
 
P

Peter Otten

Dan said:
Is there a python equivalent of this trick in C?

Logic_Test ? True_Result : False_Result

Example:
printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );
.... if n == 1:
.... return article(sg)
.... elif n == 0:
.... return "no " + plural(sg, pl)
.... else:
.... return "%d %s" % (n, plural(sg, pl))
........ if sg[:1].lower() in "aeiou": # uniforms are off-limit
.... return "an " + sg
.... else:
.... return "a " + sg
........ if pl is None:
.... return sg + "s"
.... else:
.... return pl
....
:)

Refine as needed.

Peter

PS: The trick is to not use a trick.
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top