Evil hack of the day

Dan Eble eble at ticalc.org
Wed May 26 21:46:42 EDT 1999


On 26 May, Neel Krishnaswami wrote in comp.lang.python:

NK> This leads to the evil version:
NK> 
NK> >>> def evil(n=0):
NK> ... 	n = n + 1
NK> ... 	globals()['evil'].func_defaults = (n,)
NK> ... 	return n
NK> ...


That doesn't work for me.  Observe,


Python 1.5.1 (#1, Mar 21 1999, 22:49:36)  [GCC egcs-2.91.66 19990314/Li on
linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> def evil(n=0):
...     n = n + 1
...     globals()['evil'].func_defaults = (n,)
...     return n
... 
>>> evil()
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in evil
TypeError: object has read-only attributes


-- 
Dan Eble <eble at ticalc.org>    [My other .sig's a four-liner.]





More information about the Python-list mailing list