Re: Ternary operator alternative in Ptyhon

2008-06-18 Thread 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! Allen wrote: kretik wrote: I'm sure this is a popular one, but

Re: Ternary operator alternative in Ptyhon

2008-06-18 Thread Jeffrey Froman
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 -- http://mail.python.org/mailman

Re: Ternary operator alternative in Ptyhon

2008-06-18 Thread 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") ? pa

Re: Ternary operator alternative in Ptyhon

2008-06-18 Thread Allen
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

Re: Ternary operator alternative in Ptyhon

2008-06-17 Thread Robert Lehmann
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

Re: Ternary operator alternative in Ptyhon

2008-06-17 Thread Gary Herron
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

Re: Ternary operator alternative in Ptyhon

2008-06-17 Thread William Heymann
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 che

Ternary operator alternative in Ptyhon

2008-06-17 Thread 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