Ternary operator alternative in Ptyhon

K

kretik

I'm sure this is a popular one, but after Googling for a while I
couldn't figure out how to pull this off.

Let's say I have this initializer on a class:

def __init__(self, **params):

I'd like to short-circuit the assignment of class field values passed in
this dictionary to something like this:

self.SomeField = \
params.has_key("mykey") ? params["mykey"] : None)

Obviously I know this is not actual Python syntax, but what would be the
equivalent? I'm trying to avoid this, basically:

if params.has_key("mykey"):
self.SomeField = params["mykey"]
else:
self.SomeField = None

This is not a big deal of course, but I guess my main goal is to try and
figure out of I'm not missing something more esoteric in the language
that lets me do this.

Thanks in advance.
 
R

Robert Lehmann

I'm sure this is a popular one, but after Googling for a while I
couldn't figure out how to pull this off.

Let's say I have this initializer on a class:

def __init__(self, **params):

Why not ``__init__(self, mykey=None)`` in the first place?
I'd like to short-circuit the assignment of class field values passed in
this dictionary to something like this:

self.SomeField = \
params.has_key("mykey") ? params["mykey"] : None)

Obviously I know this is not actual Python syntax, but what would be the
equivalent? I'm trying to avoid this, basically:

if params.has_key("mykey"):
self.SomeField = params["mykey"]
else:
self.SomeField = None

This is not a big deal of course, but I guess my main goal is to try and
figure out of I'm not missing something more esoteric in the language
that lets me do this.

Thanks in advance.

You're lucky -- Python 2.5 just grew a ternary if-construct. You'd use it
like that::

self.SomeField = params["mykey"] if "mykey" in params else None
# or, generically: TRUE if CONDITION else FALSE

Notice the use of the `in` operator, which is recommended over
`dict.has_key`.

HTH,
 
W

William Heymann

if params.has_key("mykey"):
self.SomeField = params["mykey"]
else:
self.SomeField = None


self.SomeField = parms.get('mykey', None)

This looks like what you want and it is also simpler because you don't deal
with checking if the key exists.
 
G

Gary Herron

kretik said:
I'm sure this is a popular one, but after Googling for a while I
couldn't figure out how to pull this off.

Let's say I have this initializer on a class:

def __init__(self, **params):

I'd like to short-circuit the assignment of class field values passed
in this dictionary to something like this:

self.SomeField = \
params.has_key("mykey") ? params["mykey"] : None)

For years, Python did not have such a thing, but recent versions support
the syntax
a if c else b

In spite of the odd ordering of that parts, they are executed (or not)
just as you like.
self.SomeField = params["mykey"] if params.has_key("mykey") else None


Gary Herron
Obviously I know this is not actual Python syntax, but what would be
the equivalent? I'm trying to avoid this, basically:

if params.has_key("mykey"):
self.SomeField = params["mykey"]
else:
self.SomeField = None

This is not a big deal of course, but I guess my main goal is to try
and figure out of I'm not missing something more esoteric in the
language that lets me do this.

Thanks in advance.
 
J

jeremie fouche

kretik a écrit :
I'm sure this is a popular one, but after Googling for a while I
couldn't figure out how to pull this off.

I'd like to short-circuit the assignment of class field values passed in
this dictionary to something like this:

self.SomeField = \
params.has_key("mykey") ? params["mykey"] : None)

Obviously I know this is not actual Python syntax, but what would be the
equivalent? I'm trying to avoid this, basically:

if params.has_key("mykey"):
self.SomeField = params["mykey"]
else:
self.SomeField = None

This is not a big deal of course, but I guess my main goal is to try and
figure out of I'm not missing something more esoteric in the language
that lets me do this.

You can also use :
self.SomeField = params.has_key("mykey") and params["mykey"] or None

But it's not easy to read
 
K

kretik

Thank you everyone. I ended up implementing the dict.get() method, which
seems "cleaner", but I'll keep the (x if y else z) syntax in mind. I
didn't know it existed, I guess it's what I was looking for to begin with.

Thanks again!
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top