Handling 3 operands in an expression without raising an exception

Νίκος nikos.gr33k at gmail.com
Fri Sep 27 07:15:19 EDT 2013


Στις 27/9/2013 1:43 μμ, ο/η Dave Angel έγραψε:
>> Στις 27/9/2013 1:55 πμ, ο/η Dave Angel έγραψε:
>>> Simply assign the default values BEFORE the try block, and use pass as
>>> the except block.
>>>
>>> That still doesn't get around the inadvisability of putting those 3
>>> lines in the try block.
>>>
>>> You still haven't dealt with the gt assignment and its possible
>>> exception.
>> Yes gi must be removed form within the try block because iam tesign it
>> for failure.
> Then why don't you do it below?
>> I'am not sure what you mean though when you say:
>>> Simply assign the default values BEFORE the try block, and use pass as
>>> the except block.
>> Can you please make it more clear for me?
>>
>> This is my code as i have it at the moment:
>>
>> ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or
>> os.environ.get('REMOTE_ADDR', "Cannot Resolve") )
>> try:
>> 	gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
>> 	city = gi.time_zone_by_addr( ipval )
>> 	host = socket.gethostbyaddr( ipval ) [0]
>> except socket.gaierror as e:
>> 	city = "Άγνωστη Πόλη"
>> 	host = "Άγνωστη Προέλευση"

> ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or
> os.environ.get('REMOTE_ADDR', "Cannot Resolve") )
> city = "Άγνωστη Πόλη"
> host = "Άγνωστη Προέλευση"
> try:
> 	city = gi.time_zone_by_addr( ipval )
> 	host = socket.gethostbyaddr( ipval ) [0]
> except socket.gaierror as e:
> 	pass

Thanks for taking the time to expain this:

In the exact above solution of yours opposed to the top of mines is that 
you code has the benefit of actually identifying the variable that 
failed to have been assigned a value while in my code no matter what 
variable failes in the try block i assign string to both 'city' and 
'host' hence i dont really know which one of them is failing?

Di i understood it correctly?



More information about the Python-list mailing list