(original) (raw)

Since we're talking about class decorators, I have a question about
function and instancemethod objects.  The following code works

class Root(object):
   def index(self):
       return "Hello World!"

   index.exposed = True

but this code

class Root(object):
   def index(self):
       return "Hello World!"
index.exposed = True

gives the following exception

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>
AttributeError: 'instancemethod' object has no attribute 'exposed'


I can tell that instancemethods can't have attributes added to
them outside of their class definition.  Is this part of the Python
language spec, or just an implementation detail of CPython?

I
bring this up here because it makes writing certain class decorators
much more annoying.  For example, if I want to write a class decorator
that will set "exposed=True" for every method of a class, I must resort
to shenanigans.


- Eli