Is there a way to access formal parameters if you implement __getattribute__

It seems to __getattribute__have only 2 parameters (self, name).

However, in the actual code, the method I intercept actually takes arguments.

Is there access to these arguments?

Thanks,

Charlie

+3
source share
7 answers

__ getattribute __ just returns the requested attribute, in the case of a method, the interface __ call __ is used to call it .

Instead of returning a method, return a wrapper around it, for example:

def __getattribute__(self, attr):
     def make_interceptor(callble):
         def func(*args, **kwargs):
             print args, kwargs
             return callble(*args, **kwargs)
         return func
     att = self.__dict__[attr]
     if callable(att):
        return make_interceptor(att)
+5
source

Python - , , . . .

, - :

def __getattribute__(self, key):
    if key == "something_interesting":
        def func(*args, **kwargs):
            # use arguments, and possibly the self variable from outer scope
        return func
    else:
        return object.__getattribute__(self, key)

, __getattribute__ . , , , . , __getattr__ ? , , , . .

+3

, , . getattribute , __dict__

def __getattribute__(self, attr):
    if attr in self.__dict__:
          return self.__dict__[attr]
    # Add your changes here
+1

, . __getattribute__ , . ( ), , .

, , (*args, **kwargs), , .

. Python.

+1

"", "wrapper" . :

class F(object):
   def __init__(self, func):
      self.func = func
      return
   def __call__(self, *args, **kw):
      print "Calling %r:" % self.func
      print "  args: %r" % (args,)
      print "  kw: %r" % kw
      return self.func(*args,**kw)

class C(object):
   def __init__(self):
      self.a = 'an attribute that is not callable.'
      return
   def __getattribute__(self,name):
      attr = object.__getattribute__(self,name)
      if callable(attr):
         # Return a callable object that can examine, and even
         # modify the arguments prior to calling the method.
         return F(attr)
      # Return the reference to any non-callable attribute.
      return attr
   def m(self, *a, **kw):
      print "hello from m!"
      return

>>> c=C()
>>> c.a
'an attribute that is not callable.'
>>> c.m
<__main__.F object at 0x63ff30>
>>> c.m(1,2,y=25,z=26)
Calling <bound method C.m of <__main__.C object at 0x63fe90>>:
  args: (1, 2)
  kw: {'y': 25, 'z': 26}
hello from m!
>>> 

Ants Aasma __getattribute__. all self, __getattribute__. self.__dict__ __getattribute__, , ! , __getattribute__ (, object.__getattribute__(self,name).)

+1

def argumentViewer(func):
    def newfunc(*args, **kwargs):
        print "Calling %r with %r %r" % (func, args, kwargs)
        return func(*args, **kwargs)
    return newfunc

class Foo(object):
    def __getattribute__(self, name):
        print "retrieving data"
        return object.__getattribute__(self, name)

    @argumentViewer
    def bar(self, a, b):
        return a+b

# Output
>>> a=Foo()
>>> a.bar
retrieving data
<bound method Foo.newfunc of <__main__.Foo object at 0x01B0E3D0>>
>>> a.bar(2,5)
retrieving data
Calling <function bar at 0x01B0ACF0> with (<__main__.Foo object at 0x01B0E3D0>, 2, 5) {}
7
0

ASk answer ( ) ,

     att = self.__dict__[attr]

att = type(self).__dict__[attr].__get__(self, type(self))

:

def getattribute(self, name):
    if name.startswith('__') and name.endswith('__'):
        return object.__getattribute__(self, name)
    s = '*** method "%s" of instance "%s" of class "%s" called with'\
        ' following'
    print s % (name, id(self), self.__class__.__name__),

    def intercept(call, name):
        def func(*args, **kwargs):
            print 'args: {}, kwargs: {} ***'.format(args, kwargs)
            c = call(*args, **kwargs)
            print '*** "%s" finished ***' % name
            return c
        return func
    a = type(self).__dict__[name].__get__(self, type(self))
    if callable(a):
        return intercept(a, name)
    return object.__getattribute__(self, name)


def inject(cls):
    setattr(cls, '__getattribute__', getattribute)
    return cls


@inject
class My_Str(object):
    def qwer(self, word='qwer'):
        print 'qwer done something here...'
0

Source: https://habr.com/ru/post/1709114/


All Articles