简体   繁体   中英

How to catch the lookup of non-existent nested variables on a Python class?

I'm trying to hijack any calls made to my class and pass them to a method of my choice on the class.

My class so far:

class Klass(object):
    def __getattribute__(self, name):
        if name == '_dummy_func':
            return object.__getattribute__(self, name)
        return object.__getattribute__(self, 'dummy_func')

    def _dummy_func(self):
        print 'dummy func called!'

which works when I do this:

cls = Klass()
cls.foo()

but falls over when trying to do this:

cls = Klass()
cls.foo.bar()

as dummy_func has no attribute bar .

I looked at trying to catch this nested behaviour in the __getattribute__() by checking to see if name is a function as described here , however it's a string not the actual variable.

Is there a way to catch this from inside Klass ?

Instead of returning a dummy function , just return a dummy object which is callable through the __call__ method and in turn defines __getattr__ to return another dummy object if an attribute is requested:

class Dummy(object):
  def __init__(self, klass, path):
    self.klass = klass
    self.path = path

  def __getattr__(self, attr):
    return Dummy(self.klass, self.path + [attr])

  def __call__(self, *args, **kw):
    self.klass._dummy_func(self.path, *args, **kw)

class Klass(object):
  def __getattr__(self, attr):
    return Dummy(self, [attr])

  def _dummy_func(self, path, *args, **kw):
    print "Dummy function %s called with %d arguments!" \
                      % ('.'.join(path), len(args))

Klass().foo.bar.func(1, 2)  
# => "Dummy function foo.bar.func called with 2 arguments!"

You can now apply your logic depending on the full path given to _dummy_func . You even get the supplied arguments and keywords to work with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM