[Tutor] Need to call a custom/magic method on calling of every Class methods

Sunil Tech sunil.techspk at gmail.com
Thu Dec 17 09:36:10 EST 2015


Oh!

Thank you so much Steven for the reply.

On Thu, Dec 17, 2015 at 7:03 PM, Steven D'Aprano <steve at pearwood.info>
wrote:

> 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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list