Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Programming] Am I the only one...?
#6
Fiel Wrote:There's a way around that.

value = dict.get(keyName, defaultValue)

So...

Dict = {'1':'my value'}
value = Dict.get('1', 'NULL') #Returns 'my value'
value = Dict.get('2', 'NULL') #Returns 'NULL'

Or you can do...

Code:
if key in Dict:
   #Do something
else:
   #do something else

No try/except required. Smile

ALL Python code I write has try/catch exceptions as a last resort because I consider it to be poor programming.
Oh, I know. But dict[key] should return null/nil when key's not found! Sad

 Also...
Reply


Messages In This Thread
[Programming] Am I the only one...? - by Fiel - 2009-09-30, 06:45 PM
[Programming] Am I the only one...? - by Nikkey - 2009-09-30, 07:03 PM
[Programming] Am I the only one...? - by Fiel - 2009-09-30, 07:09 PM
[Programming] Am I the only one...? - by Cyadd - 2009-09-30, 07:30 PM
[Programming] Am I the only one...? - by Spaz - 2009-09-30, 07:45 PM
[Programming] Am I the only one...? - by Nikkey - 2009-09-30, 07:49 PM
[Programming] Am I the only one...? - by Fiel - 2009-09-30, 08:13 PM
[Programming] Am I the only one...? - by AngelSL - 2009-10-04, 01:30 AM

Forum Jump:


Users browsing this thread: