Search code examples
pythoncherrypymetaobject

In python, What does it mean if I assign a variable to a method?


Reading the CherryPy tutorial I run into this

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

So was does it mean? is exposed a variable in the local scope of the method index? If so, can I the value of expose change? I think it has something to do with python's MetaObject protocol to expose a class definition as an object itself.


Solution

  • No it's not a local variable of function index, rather it's a attribute of that function . And yes you can modify it outside the function too.

    Read the PEP 232 on function attributes.

    example:

    In [2]: def foo():pass
       ...: 
    
    In [3]: foo.bar="text"
    
    In [4]: foo.bar
    Out[4]: 'text'