Confused with getattribute and setattribute in python

后端 未结 4 887
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 01:27

I want to know that if I have class like this

class Test(object):
    def __init__(self):
        self.a = 20
        self.b = 30

obj = Test()
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 01:47

    class test():
        def __init__(self):
            self.a = 1
    
        def __getattribute__(self, attr):
            print 'Getattribute:',attr
    
        def __getattr__(self, attr):
            print 'GetAttr:',attr
    
        def __dict__(self, attr):
            print 'Dict:',attr
    
        def __call__(self, args=None):
            print 'Called:',args
    
        def __getitem__(self, attr):
            print 'GetItem:',attr
    
        def __get__(self, instance, owner):
            print 'Get:',instance,owner
    
        def __int__(self):
            print 'Int'
    
    
    
    x = test()
    print x.a
    

    None of the above will be called..

    [root@faparch doxid]# python -m trace --trace test_dict.py
     --- modulename: test_dict, funcname: 
    test_dict.py(1): class test():
     --- modulename: test_dict, funcname: test
    test_dict.py(1): class test():
    test_dict.py(2):    def __init__(self):
    test_dict.py(5):    def __getattribute__(self, attr):
    test_dict.py(8):    def __getattr__(self, attr):
    test_dict.py(11):   def __dict__(self, attr):
    test_dict.py(14):   def __call__(self, args=None):
    test_dict.py(17):   def __getitem__(self, attr):
    test_dict.py(20):   def __get__(self, instance, owner):
    test_dict.py(23):   def __int__(self):
    test_dict.py(28): x = test()
     --- modulename: test_dict, funcname: __init__
    test_dict.py(3):        self.a = 1
    test_dict.py(29): print x.a
    1
     --- modulename: trace, funcname: _unsettrace
    trace.py(80):         sys.settrace(None)
    

    You'd might want to look into: http://docs.python.org/2/library/numbers.html#numbers.Number Most likely you'll need to implement a nested class that handles the number-class functions in order to snatch up calls such as in the example. Or, at least that's one way of doing it..

    The integer value contains the following functions which you have to intercept

    ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
    

提交回复
热议问题