Disclaimer This is just an exercise in meta-programming, it has no practical purpose.
I've assigned __getitem__
and __getattr__
methods on a function object, but
there is no effect...
def foo():print "foo!"foo.__getitem__ = lambda name: name
foo.__getattr__ = lambda name: name
foo.baz = 'baz'
Sanity check that we can assign properties to a function:
>>> foo.baz
'baz'
Neat. How about the "magic getters"?
>>> foo.bar
Traceback (most recent call last):File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'bar'>>> foo['foo']
Traceback (most recent call last):File "<stdin>", line 1, in <module>
TypeError: 'function' object is not subscriptable>>> getattr(foo, 'bar')
Traceback (most recent call last):File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'bar'
Is it possible to have a "magic getter" on a function object?