<div dir="ltr">Thank you very much Gerhard and Terry.<br><div class="gmail_quote"><div dir="ltr"><br>I am trying to add undefined state to some Boolean operator. Here is what I tried to do and It is not working:<br><br>class _3ph:<br>
    def __init__(self):<br>        self.value = 0<br>
<br>    def __xor__(self,item):<br>        if self.value==2 or item==2:<br>             return 2<br>        else:<br>             return self.__xor__(item)<br><br>what I am trying to do is assigning 2 to undefined state and have xor operator return 2 if one of inputs are 2.<br>

it seems Although I defined xor in _3ph class, python treat any object from this class just like integer variables. <br>can you help me find what is wrong here?<br><br>Cheers,<br><font color="#888888">Arash</font><div><div>
</div><div class="Wj3C7c"><br><br><div class="gmail_quote">On Tue, Sep 23, 2008 at 11:06 AM, Terry Reedy <span dir="ltr"><<a href="mailto:tjreedy@udel.edu" target="_blank">tjreedy@udel.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div>Arash Arfaee wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi All,<br>
<br>
Is there anyway to add new in-place operator to Python? Or is there any way to redefine internal in-place operators?<br>
</blockquote>
<br></div>
Python does not have 'in-place operators'.  It has 'augmented assignment statements' that combines a binary operation with an assignment.  *If* the target being rebound is mutable, *then* (and only then) the operation can be and is recommended to be done 'in-place'.  User-defined mutable classes (as most are) can implement in-place behavior with __ixxx__ methods.  But for the reason given below, __ixxx__ methods should supplement and not replace direct mutation methods.<br>


<br>
Correct terminology is important for understanding what augmented assigments do and what they are basically about.<br>
<br>
First, most augmented assignments target immutables, in particular, numbers and strings, which do not have __ixxx__ methods.  So the operation is *not* done in-place.  The only difference from separately indicating the assignment and operation is that the programmer writes the target expression just once and the interpreter evaluates the target expression just once instead of each repeating themselves.  (And consequently, any side-effects of that evaluation happen just once instead of twice.)  The same __xxx__ or __rxxx__ method is used in either case.  This non-repetition is the reason for augmented assigments.  The optional in-place optimization for mutables is secondary.  It was debated and could have been left out.<br>


<br>
Second, all augmented assignments perform an assignment, even if the operation is done in place.  However, if a mutable such as a list is accessed as a member of an immutable collection such as a tuple, mutation is possible, but rebinding is not.  So the mutation is done and then an exception is raised.  To avoid the exception, directly call a mutation method such as list.extend.<br>


<br>
Terry Jan Reedy<div><div></div><div><br>
<br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br></div></div></div>
</div><br></div>