Handling 3 operands in an expression without raising an exception
Νίκος
nikos.gr33k at gmail.com
Thu Sep 26 03:26:48 EDT 2013
Hello,
How can i wrote the two following lines so for NOT to throw out
KeyErrors when a key is missing?
city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or "Άγνωστη Πόλη"
host = socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or
socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or "Άγνωστη Προέλευση"
I was under the impression that the 'or' operator was handling this in
case one operand was failing but its not the case here.
I believe in a KeyError is missing the expression cannot even be
evaluates as Truthy or Falsy.
Then i thought of os.environ.get() to default to something but then
again we have 3 operand in the expression.
More information about the Python-list
mailing list