def getvalue(dictionary, name):
try:
return dictionary[name]
except Exception as e:
log = open('logfile.txt', 'w')
log.write('%s\n' % e)
log.close()
What does this code do? (I understand all of it except for the log.write part, no need to explain the rest, I just added the rest for context)
This code is roughly equivalent to
dictionary.get(name), except that in the case when a non-existent key is looked up, the name of the non-existent key is also written to filelogfile.txt. Similarly todict.get, the objectNonewill be returned by the function in this case, and the exception will not be re-raised.You can clear things up for yourself by experimenting in the interpreter: