title: Python function attributes - uses and abuses
body: <p>Not many are aware of this feature, but Python's functions (and methods) can have <a href="http://www.python.org/dev/peps/pep-0232/" rel="noreferrer">attributes</a>. Behold:</p>
  
  <pre><code>&gt;&gt;&gt; def foo(x):
  ...     pass
  ...     
  &gt;&gt;&gt; foo.score = 10
  &gt;&gt;&gt; dir(foo)
  ['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__', '__getattribute__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name', 'score']
  &gt;&gt;&gt; foo.score
  10
  &gt;&gt;&gt; foo.score += 1
  &gt;&gt;&gt; foo.score
  11
  </code></pre>
  
  <p>What are the possible uses and abuses of this feature in Python ? One good use I'm aware of is <a href="http://www.dabeaz.com/ply/" rel="noreferrer">PLY</a>'s usage of the docstring to associate a syntax rule with a method. But what about custom attributes ? Are there good reasons to use them ?</p>