[Tutor] Need to call a custom/magic method on calling of every Class methods
Steven D'Aprano
steve at pearwood.info
Thu Dec 17 08:33:21 EST 2015
On Thu, Dec 17, 2015 at 05:18:26PM +0530, Sunil Tech wrote:
> Hi Tutor,
>
> I am searching for any magic method/or any method that calls on every
> calling of the Class methods.
[...]
> Is there any magic method that is auto called on every call of these Class
> Methods?
No, not really, but you can get *close* to what you want. You can't
intercept *calling* the methods, but you can intercept the attribute
lookup:
class A(object):
def __getattribute__(self, name):
attr = super(A, self).__getattribute__(name)
if callable(attr):
print(name)
return attr
def one(self):
return 1
def two(self):
return 2
But beware that this will slow down all attribute access.
This is possibly the simplest solution. Perhaps a better solution would
be to create a custom Method descriptor, and then modify the class to
use this descriptor instead of standard methods. But this is much
more work.
--
Steve
More information about the Tutor
mailing list