New assignmens ...

Chris Angelico rosuav at gmail.com
Wed Oct 27 20:50:08 EDT 2021


On Thu, Oct 28, 2021 at 11:08 AM Avi Gross via Python-list
<python-list at python.org> wrote:
>
> Dave,
>
> You make me wonder about unintended side effects. Are we allowing the ++ and
> --- operations into Python through a side door?
>

class IncrementableInteger(int):
    def __pos__(self): return HalfIncremented(self)
    def __neg__(self): return HalfDecremented(self)

class HalfIncremented(IncrementableInteger):
    def __pos__(self): return IncrementableInteger(self + 1)

class HalfDecremented(IncrementableInteger):
    def __neg__(self): return IncrementableInteger(self - 1)

Up to you to make the actual mutation work but have fun making a
nightmare for subsequent maintainers!

ChrisA
PS. Nobody's ever stopping you from def __add_(self, other): return
self * other + 3
Just sayin'.


More information about the Python-list mailing list