type lookuperror

Peter Otten __peter__ at web.de
Thu Aug 18 03:27:13 EDT 2016


meInvent bbird wrote:

> would like to check errors for every function i run,
> got error type lookuperror
> 
> def checkexception(func, **kwargs):
>     try:
>         result = func(*tuple(value for _, value in kwargs.iteritems()))

You are turning keyword arguments into positional arguments. The order of 
entries in a dict is undefined and may even vary between runs of python:

$ PYTHONHASHSEED=random python -c 'print dict(foo=1, bar=2, baz=3).items()'
[('baz', 3), ('foo', 1), ('bar', 2)]
$ PYTHONHASHSEED=random python -c 'print dict(foo=1, bar=2, baz=3).items()'
[('baz', 3), ('foo', 1), ('bar', 2)]
$ PYTHONHASHSEED=random python -c 'print dict(foo=1, bar=2, baz=3).items()'
[('baz', 3), ('bar', 2), ('foo', 1)]
$ PYTHONHASHSEED=random python -c 'print dict(foo=1, bar=2, baz=3).items()'
[('foo', 1), ('baz', 3), ('bar', 2)]

If you want to turn keyword arguments into positional arguments you have to 
think about a mechanism to ensure the intended order.

>     except:
>         print "Unexpected error:", sys.exc_info()[0]
>         try:
>             print(func.__doc__)
>         except:
>             print("no doc error")





More information about the Python-list mailing list