Skip to main content
deleted 4 characters in body
Source Link
jonrsharpe
  • 123.3k
  • 31
  • 277
  • 488

As mentioned elsewhere a possible issue with dct.get(key, default) is that, if key in dct but dct[key] is None, you get None not your default:

>>> dct = dict(foo=123, baz=None) >>> dct.get("foo", 0) 123 >>> dct.get("bar", 0) 0 >>> dct.get("baz", 0) >>> # returnswhere'd None,the notvalue 0go? 

Since Python 3.8, using the "walrus operator" to create an assignment expression, you can handle this like:

>>> value if (value := dct.get("baz")) is not None else 0 0 

As mentioned elsewhere a possible issue with dct.get(key, default) is that, if key in dct but dct[key] is None, you get None not your default:

>>> dct = dict(foo=123, baz=None) >>> dct.get("foo", 0) 123 >>> dct.get("bar", 0) 0 >>> dct.get("baz", 0) >>> # returns None, not 0 

Since Python 3.8, using the "walrus operator" to create an assignment expression, you can handle this like:

>>> value if (value := dct.get("baz")) is not None else 0 0 

As mentioned elsewhere a possible issue with dct.get(key, default) is that, if key in dct but dct[key] is None, you get None not your default:

>>> dct = dict(foo=123, baz=None) >>> dct.get("foo", 0) 123 >>> dct.get("bar", 0) 0 >>> dct.get("baz", 0) >>> # where'd the value go? 

Since Python 3.8, using the "walrus operator" to create an assignment expression, you can handle this like:

>>> value if (value := dct.get("baz")) is not None else 0 0 
deleted 6 characters in body
Source Link
robertspierre
  • 5.4k
  • 3
  • 43
  • 65

As mentioned elsewhere a possible issue with dct.get(key, default) is that, if key in dct but dct[key] is None, you get None not your default:

>>> dct = dict(foo=123, baz=None) >>> dct.get("foo", 0) 123 >>> str(dct.get("bar", 0) 0 >>> dct.get("baz", 0) >>> # where'dreturns theNone, valuenot go?0 

Since Python 3.8, using the "walrus operator" to create an assignment expression, you can handle this like:

>>> value if (value := dct.get("baz")) is not None else 0 0 

As mentioned elsewhere a possible issue with dct.get(key, default) is that, if key in dct but dct[key] is None, you get None not your default:

>>> dct = dict(foo=123, baz=None) >>> dct.get("foo", 0) 123 >>> str(dct.get("bar", 0) 0 >>> dct.get("baz", 0) >>> # where'd the value go? 

Since Python 3.8, using the "walrus operator" to create an assignment expression, you can handle this like:

>>> value if (value := dct.get("baz")) is not None else 0 0 

As mentioned elsewhere a possible issue with dct.get(key, default) is that, if key in dct but dct[key] is None, you get None not your default:

>>> dct = dict(foo=123, baz=None) >>> dct.get("foo", 0) 123 >>> dct.get("bar", 0) 0 >>> dct.get("baz", 0) >>> # returns None, not 0 

Since Python 3.8, using the "walrus operator" to create an assignment expression, you can handle this like:

>>> value if (value := dct.get("baz")) is not None else 0 0 
Source Link
jonrsharpe
  • 123.3k
  • 31
  • 277
  • 488

As mentioned elsewhere a possible issue with dct.get(key, default) is that, if key in dct but dct[key] is None, you get None not your default:

>>> dct = dict(foo=123, baz=None) >>> dct.get("foo", 0) 123 >>> str(dct.get("bar", 0) 0 >>> dct.get("baz", 0) >>> # where'd the value go? 

Since Python 3.8, using the "walrus operator" to create an assignment expression, you can handle this like:

>>> value if (value := dct.get("baz")) is not None else 0 0