Re: a dict trick

2007-08-02 Thread Michael J. Fromberger
In article <[EMAIL PROTECTED]>, james_027 <[EMAIL PROTECTED]> wrote: > hi > > for example I have this dictionary > > dict = {'name':'james', 'language':'english'} > > value = 'sex' in dict and dict['sex'] or 'unknown' > > is a right pythonic of doing this one? I am trying to get a value from

Re: a dict trick

2007-08-02 Thread Bruno Desthuilliers
james_027 a écrit : > Hi, > > what if we're not dealing with dict? is there a pythonic way of doing > ternary? the bool ? x:y Python 2.5 introduced the following syntax: expr1 if condition else expr2 In older Python versions, one has to use and/or (like you wrongly did) or tuple/dict dispatch

Re: a dict trick

2007-08-02 Thread Bruno Desthuilliers
james_027 a écrit : > hi > > for example I have this dictionary > > dict = {'name':'james', 'language':'english'} > > value = 'sex' in dict and dict['sex'] or 'unknown' > > is a right pythonic of doing this one? No. The first problem is that using 'dict' as an identifier, you're shadowing the

Re: a dict trick

2007-08-02 Thread james_027
Hi, what if we're not dealing with dict? is there a pythonic way of doing ternary? the bool ? x:y Thanks james -- http://mail.python.org/mailman/listinfo/python-list

Re: a dict trick

2007-08-02 Thread Alex Popescu
james_027 <[EMAIL PROTECTED]> wrote in news:1186036331.304916.304020 @e9g2000prf.googlegroups.com: > hi > > for example I have this dictionary > > dict = {'name':'james', 'language':'english'} > > value = 'sex' in dict and dict['sex'] or 'unknown' > > is a right pythonic of doing this one? I a

Re: a dict trick

2007-08-02 Thread [EMAIL PROTECTED]
james_027 wrote: > hi > > for example I have this dictionary > > dict = {'name':'james', 'language':'english'} > > value = 'sex' in dict and dict['sex'] or 'unknown' > > is a right pythonic of doing this one? I am trying to get a value from > the dict, but if the key doesn't exist I will provid

Re: a dict trick

2007-08-02 Thread Gary Herron
james_027 wrote: > hi > > for example I have this dictionary > > dict = {'name':'james', 'language':'english'} > > value = 'sex' in dict and dict['sex'] or 'unknown' > > is a right pythonic of doing this one? I am trying to get a value from > the dict, but if the key doesn't exist I will provide on

Re: a dict trick

2007-08-02 Thread Stargaming
On Thu, 02 Aug 2007 06:32:11 +, james_027 wrote: > hi > > for example I have this dictionary > > dict = {'name':'james', 'language':'english'} First of all, this is a bad name because it shadows (overwrites) the reference to the builtin constructor, `dict`. > value = 'sex' in dict and dic

a dict trick

2007-08-01 Thread james_027
hi for example I have this dictionary dict = {'name':'james', 'language':'english'} value = 'sex' in dict and dict['sex'] or 'unknown' is a right pythonic of doing this one? I am trying to get a value from the dict, but if the key doesn't exist I will provide one. THanks james -- http://mail