Posts

Showing posts from March, 2014

Weakref proxy is for instance only ...

Proxy cannot be created for basic datatypes like list, dict, str and int. Proxy can be create for class instances only. Also proxy cannot be created for proxy of another instance. >>> import weakref >>> a = 'str' >>> b = weakref.proxy(a) Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: cannot create weak reference to 'str' object >>> a = 1 >>> b = weakref.proxy(a) Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: cannot create weak reference to 'int' object >>> a = [] >>> b = weakref.proxy(a) Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: cannot create weak reference to 'list' object >>> a = {} >>> b = weakref.proxy(a) Traceback (most recent call last):   File "<stdin>",

Raising vs Reraising exceptions in python

>>> def foo(x): ...    raise Exception('%s is invalid input' % x) ... >>> try: ...     foo(1) ... except Exception, e: ...     raise e                          # RE-RAISING EXCEPTIONS ... Traceback (most recent call last):   File "<stdin>", line 4, in <module>    # LOSES foo Exception: 1 is invalid input >>> try: ...     foo(1) ... except Exception: ...     raise                             # RAISING EXCEPTIONS ... Traceback (most recent call last):   File "<stdin>", line 2, in <module>   File "<stdin>", line 2, in foo          # STORES foo too Exception: 1 is invalid input >>>