Reputation: 165
Lets say I have a method which I decorate with @property
.
Is there a way to get the underlying name of the decorated method?
How could I for instance print the name of the property when it does not have a __name__
attribute?
I'm trying to do a RPC call and everything works for methods and static functions but not this.
Upvotes: 1
Views: 322
Reputation: 22992
A property has the following attributes: fget, fset and fdel, which are references to the getter/setter/deleter functions.
So, you can get the __name__
of each of this functions (if not None
)
Demo:
class Thing(object):
@property
def foo(self):
return "foo"
def get_baz(self):
return "baz"
bar = property(get_baz)
>>> Thing.foo.fget.__name__
'foo'
>>> Thing.bar.fget.__name__
'get_baz'
Upvotes: 3
Reputation: 123501
If you use functools.wraps()
in your decorator, the decorated function will have the __name__
of the wrapped function.
IF you had some sample code for the decorator in your question, I could show more precisely how to do this...
Upvotes: -1
Reputation: 696
i think if use decorator @property you can access your function with name of function and you can give return value.
class Foo:
value = 'Foo'
def setName(self, name):
self.value = name
@property
def name(self):
return "You name is: {}".format(self.value)
foo = Foo()
print(foo.name)
foo.setName('Bar')
print(foo.name)
Upvotes: -1