Prev: Failure in test_hashlib.py and test_hmac.py
Next: Partly erratic wrong behaviour, Python 3, lxml
From: Андрей Симурзин on 4 Mar 2010 03:25 It is object of the class A, in conteiner's class tmpA. Not all method from A are in the tmpA. So for exapmle: A + B -- yes , tmpA + B no. I try to call method from A for tmpA. I can to call simple method, such as change(), bit __add__ - don't work. If to remove inheritance from object, the code work's. Help me, please #-------------------------------------- class A(object): def __init__( self, x, y ): self.x = x self.y = y pass #------- def __add__( self, arg ): tmp1 = self.x + arg.x tmp2 = self.y + arg.y return tmpA( A( tmp1, tmp2 ) ) def change( self, x, y ): self.x = x self.y = y pass pass #------------------------------------------ class tmpA( object ): def __init__( self, theA ): self.A = theA pass #------- def __call__( self ): return self.A #------- def __coerce__( self, *args ): return None #------- def __getattr__( self, *args ): name = args[ 0 ] try: attr = None exec "attr = self.__call__().%s" % name return attr except : raise AttributeError #-------------------------------------- class B( object ): def __init__( self, x, y): self.x = x self.y = y pass #------------------------------------- a=A( 1,2 ) b=B( 3,4 ) tmp_a = a + b #well tmp_a.change( 0, 0 ) # very well !!! v = tmp_a + b #TypeError: "unsupported operand type(s) for +: 'tmpA' and 'B'"
From: Chris Rebert on 4 Mar 2010 03:38 On Thu, Mar 4, 2010 at 12:25 AM, ÐндÑей СимÑÑзин <asimurzin(a)gmail.com> wrote: > It is object of the class A, in conteiner's class tmpA. Not all method > from A are in the tmpA. So for exapmle: > A + B -- yes , tmpA + B no. I try to call method from A for tmpA. I > can to call simple method,  such as change(), bit __add__ -  don't > work. If to remove inheritance from object, the code work's. Help me, > please Some clarity has been lost in translation, but I think I get what you're saying. __add__ and the other double-underscore special methods are not looked up using __getattr__ or __getattribute__, hence trying to do addition on tmpA, which does not define an __add__ method, fails. For a full explanation, read: http://docs.python.org/reference/datamodel.html#special-method-lookup-for-new-style-classes Cheers, Chris -- http://blog.rebertia.com
From: Andrey Simurzin on 4 Mar 2010 03:54 On 4 маÑ, 11:38, Chris Rebert <c...(a)rebertia.com> wrote: > On Thu, Mar 4, 2010 at 12:25 AM, ÐндÑей СимÑÑзин <asimur...(a)gmail.com> wrote: > > It is object of the class A, in conteiner's class tmpA. Not all method > > from A are in the tmpA. So for exapmle: > > A + B -- yes , tmpA + B no. I try to call method from A for tmpA. I > > can to call simple method,  such as change(), bit __add__ -  don't > > work. If to remove inheritance from object, the code work's. Help me, > > please > > Some clarity has been lost in translation, but I think I get what you're saying. > __add__ and the other double-underscore special methods are not looked > up using __getattr__ or __getattribute__, hence trying to do addition > on tmpA, which does not define an __add__ method, fails. > > For a full explanation, read:http://docs.python.org/reference/datamodel.html#special-method-lookup... > > Cheers, > Chris > --http://blog.rebertia.com Thank you very much
|
Pages: 1 Prev: Failure in test_hashlib.py and test_hmac.py Next: Partly erratic wrong behaviour, Python 3, lxml |