By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,761 Members | 1,071 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,761 IT Pros & Developers. It's quick & easy.

Ternary operator alternative in Ptyhon

P: n/a
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.
Jun 27 '08 #1
Share this Question
Share on Google+
7 Replies

P: n/a
On Wednesday 18 June 2008, kretik wrote:
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.
Jun 27 '08 #2

P: n/a
On Tue, 17 Jun 2008 23:18:51 -0700, kretik wrote:
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,

--
Robert "Stargaming" Lehmann
Jun 27 '08 #3

P: n/a
kretik wrote:
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.
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #4

P: n/a
kretik wrote:
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.
The syntax is a bit different, but:

result = (true_value if condition else false_value)

is how it is in Pytthon:

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

Brian Vanderburg II
Jun 27 '08 #5

P: n/a
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
--
Jérémie
Jun 27 '08 #6

P: n/a
jeremie fouche wrote:
You can also use :
self.SomeField = params.has_key("mykey") and params["mykey"] or None
Have caution with this solution: it may not provide the desired result in
the case where params["mykey"] is a false value, such as 0, or []
Jeffrey
Jun 27 '08 #7

P: n/a
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!

Allen wrote:
kretik wrote:
>I'm sure this is a popular one, but after Googling for a while I
couldn't figure out how to pull this off.
Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.