Is it possible to assign property on a function object the similar way we assign it on class instances. My desired behaviour is like this
def prop():
print("I am a property")
def my_func():
print("Just a function call")
my_func.prop = prop
my_func.prop # prints 'I am a property'
I am able to invoke it as a function call my_func.prop()
, but is there a way to override __getattribute__
or something to achieve this result?
I have tried attaching it on a class
setattr(my_func.__class__, "prop", property(prop))
but definitely that's not the way
TypeError: cannot set 'prop' attribute of immutable type 'function'
Limitations
So basically Python does not allow to do the described thing as
a) it is not possible to create your own "function" class
Base class "FunctionType" is marked final and cannot be subclassed
or
because
TypeError: type 'function' is not an acceptable base type
b) it is not possible to modify existing built-in
function
typePossible solution
Create your own
Callable
class which would be a wrapper around your functionand then assign property as you would on any other class
Example above is just for my question with dynamic assignment, you probably would you just
@property
decorator on a method within class definition.