[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-list
Subject:    Re: Printing the arguments of an attribute in a class
From:       Arnaud Delobelle <arnodel () googlemail ! com>
Date:       2010-02-28 17:51:44
Message-ID: m2r5o5s0y7.fsf () googlemail ! com
[Download RAW message or body]

vsoler <vicente.soler@gmail.com> writes:

> I have a class that is a wrapper:
>
> class wrapper:
>     def __init__(self, object):
>         self.wrapped = object
>     def __getattr__(self, attrname):
>         print 'Trace: ', attrname
>         #print arguments to attrname, how?
>         return getattr(self.wrapped, attrname)
>
> I can run it this way:
>
>>>> x = wrapper([1,2,3])
>>>> x.append(4)
> Trace:  append
>>>> x.wrapped
> [1, 2, 3, 4]
>
> I am able to capture the attribute name to x (that is, append).
> However, I do not know how to capture and print all of its arguments
> (in this case number 4).
>
> How should I proceed?
>
> Thank you

You could do something like this:

class wrapper:
    def __init__(self, object):
        self.wrapped = object
    def __getattr__(self, attrname):
        print '** get attribute: ', self.wrapped, attrname
        return wrapper(getattr(self.wrapped, attrname))
    def __call__(self, *args, **kwargs):
        print '** call with args: ', self.wrapped, args, kwargs
        return wrapper(self.wrapped(*args, **kwargs))

x = wrapper([1,2,3])
x.append(4)

I haven't thought about it too much though.

-- 
Arnaud
-- 
http://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic