469,299 Members | 2,066 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

How to do "new_variable = (variable) ? True : False;" (php) on python?

Hello to all,

How can I do

new_variable = (variable) ? True : False;

in Python in one line?

I want to do something like this:

dic = {'item1': (variable) ? True-part : False-part}

Any suggestions?

Daniel

Nov 22 '05 #1
10 4917
Daniel Crespo wrote:
How can I do

new_variable = (variable) ? True : False;

in Python in one line?


new_variable = variable

:-)

Peter
Nov 22 '05 #2
Daniel Crespo wrote:
How can I do

new_variable = (variable) ? True : False;

in Python in one line?


new_variable = variable

:-)

Peter
Nov 22 '05 #3
Daniel Crespo wrote:
Hello to all,

How can I do

new_variable = (variable) ? True : False;

in Python in one line?

I want to do something like this:

dic = {'item1': (variable) ? True-part : False-part}

Any suggestions?

Daniel


There's a trick using the short-circuiting boolean logic operators to
emulate the ternary operator in Python. Basically, you do
TEST and TRUE_PART or FALSE_PART

However, this fails if TRUE_PART evaluates to a False value; you end up
with FALSE_PART's value instead.

This is a trick/hack, though, and shouldn't really be used much - use
an if statement instead, or wait til 2.5 when an if expression is
coming in.

Nov 22 '05 #4
Daniel Crespo wrote:
Hello to all,

How can I do

new_variable = (variable) ? True : False;

in Python in one line?

I want to do something like this:

dic = {'item1': (variable) ? True-part : False-part}

Any suggestions?

Daniel


There's a trick using the short-circuiting boolean logic operators to
emulate the ternary operator in Python. Basically, you do
TEST and TRUE_PART or FALSE_PART

However, this fails if TRUE_PART evaluates to a False value; you end up
with FALSE_PART's value instead.

This is a trick/hack, though, and shouldn't really be used much - use
an if statement instead, or wait til 2.5 when an if expression is
coming in.

Nov 22 '05 #5
Peter Otten wrote:
Daniel Crespo wrote:
new_variable = (variable) ? True : False;
in Python in one line?
new_variable = variable


Of course to answer your actual question:

new_variable = variable and True or False

But you should consider that Peter has given
you a better answer than you think.

<yoda>
Don't try to force everything to the type you expect,
use things as they are; embrace duck-typing.
</yoda>
:-)

--Scott David Daniels
sc***********@acm.org
Nov 22 '05 #6
Peter Otten wrote:
Daniel Crespo wrote:
new_variable = (variable) ? True : False;
in Python in one line?
new_variable = variable


Of course to answer your actual question:

new_variable = variable and True or False

But you should consider that Peter has given
you a better answer than you think.

<yoda>
Don't try to force everything to the type you expect,
use things as they are; embrace duck-typing.
</yoda>
:-)

--Scott David Daniels
sc***********@acm.org
Nov 22 '05 #7
On Fri, 18 Nov 2005 14:15:06 -0800 in comp.lang.python, Scott David
Daniels <sc***********@acm.org> wrote:

<yoda>
Don't try to force everything to the type you expect,
use things as they are; embrace duck-typing.
</yoda>
Wrong, the grammar you have.
Do not Everything to the type you expect force.
Do or do not, there is no try.
Things as they are use.
Duck-typing embrace.
:-)


Ditto. Regards,
-=Dave

--
Change is inevitable, progress is not.
Nov 22 '05 #8
On Fri, 18 Nov 2005 14:15:06 -0800 in comp.lang.python, Scott David
Daniels <sc***********@acm.org> wrote:

<yoda>
Don't try to force everything to the type you expect,
use things as they are; embrace duck-typing.
</yoda>
Wrong, the grammar you have.
Do not Everything to the type you expect force.
Do or do not, there is no try.
Things as they are use.
Duck-typing embrace.
:-)


Ditto. Regards,
-=Dave

--
Change is inevitable, progress is not.
Nov 22 '05 #9

Peter Otten wrote:
Daniel Crespo wrote:
How can I do

new_variable = (variable) ? True : False;

in Python in one line?


new_variable = variable

:-)


I would assume the OP is just lazy and don't want to type :

new_variable = (variable_is_true_value) ?
some_expression_when_variable_is_true_value is true :
some_expression_when_variable_is_true_value is false

rather than simple True/False.

Nov 22 '05 #10

Peter Otten wrote:
Daniel Crespo wrote:
How can I do

new_variable = (variable) ? True : False;

in Python in one line?


new_variable = variable

:-)


I would assume the OP is just lazy and don't want to type :

new_variable = (variable_is_true_value) ?
some_expression_when_variable_is_true_value is true :
some_expression_when_variable_is_true_value is false

rather than simple True/False.

Nov 22 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.