1

I want to make a class' functions verbose without adding logging / prints in each one.

As an example I created a verbose class:

In [2]: class Verbose(object): 
   ...:     def __init__(self, name='default name'): 
   ...:         self._name = name 
   ...:     def __enter__(self, *a, **kw): 
   ...:         print('Entering <{}>'.format(self._name)) 
   ...:     def __exit__(self, *a, **kw): 
   ...:         print ('Exiting <{}>'.format(self._name)) 
In [5]: with Verbose('hi'): 
   ...:     pass 
   ...:                                                                                                                                                                  
Entering <hi>
Exiting <hi>

This is really helpful in the logs, as I can see each with I used. However, my code is full of methods, and I want a sort of template function, that each function would "inherit" (is that even possible in Python? function inheritance?) that would be verbose.

I'm thinking of sort of a super-class for functions, that I can override methods such as on-call and on-exit. Is that possible?

How I imagine it will look at the end:

Class Foo(object):
    def x(self):
       print ('doing things...')

Foo().x()
... <Foo.x> was called
... doing things
... <Foo.x> has finished
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124

0 Answers0