A Bug By Any Other Name ...
rwwh
rob.hooft at gmail.com
Thu Jul 16 05:25:27 EDT 2009
On Jul 7, 2:00 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Mon, 06 Jul 2009 22:18:20 -0700, Chris Rebert wrote:
> >> Not so rare. Decimal uses unary plus. Don't assume +x is a no-op.
> [...]
> > Well, yes, but when would you apply it twice in a row?
>
> My point was that unary + isn't a no-op, and therefore neither is ++. For
> Decimal, I can't think why you'd want to apply ++x, but for other
> objects, who knows?
>
> Here's a toy example:
>
> >>> class Spam(str):
>
> ... def __pos__(self):
> ... return self.__class__("spam " + self)
> ...>>> s = Spam("")
> >>> ++++s
>
> 'spam spam spam spam '
Here's another toy example:
class Toy(int):
def __init__(self, value):
self._incrd = False
int.__init__(self, value)
def incrHalf(self):
self._incrd = True
def __pos__(self):
if self._incrd:
return self.__class__(self+1)
else:
p = self.__class__(self)
p.incrHalf()
return p
def __add__(self, other):
return self.__class__(int(self)+other)
nows122[126]~% python -i toy.py
>>> i=Toy(5)
>>> +i
5
>>> ++i
6
>>> +++i
6
>>> +i++i
10
>>> +(+i++i)
10
>>> (++i)++i
11
More information about the Python-list
mailing list