0

i know some of this,ex.

__mod__ will be call /

__eq__will be call == > and <

but i don't know all.

    def __nonzero__(self):
        # an image is "true" if it contains at least one non-zero pixel
        return self.im.getbbox() is not None
    def __abs__(self):
        return self.apply("abs", self)
    def __pos__(self):
        return self
    def __neg__(self):
        return self.apply("neg", self)

    # binary operators
    def __add__(self, other):
        return self.apply("add", self, other)
    def __radd__(self, other):
        return self.apply("add", other, self)
    def __sub__(self, other):
        return self.apply("sub", self, other)
    def __rsub__(self, other):
        return self.apply("sub", other, self)
    def __mul__(self, other):
        return self.apply("mul", self, other)
    def __rmul__(self, other):
        return self.apply("mul", other, self)
    def __div__(self, other):
        return self.apply("div", self, other)
    def __rdiv__(self, other):
        return self.apply("div", other, self)
    def __mod__(self, other):
        return self.apply("mod", self, other)
    def __rmod__(self, other):
        return self.apply("mod", other, self)
    def __pow__(self, other):
        return self.apply("pow", self, other)
    def __rpow__(self, other):
        return self.apply("pow", other, self)

    # bitwise
    def __invert__(self):
        return self.apply("invert", self)
    def __and__(self, other):
        return self.apply("and", self, other)
    def __rand__(self, other):
        return self.apply("and", other, self)
    def __or__(self, other):
        return self.apply("or", self, other)
    def __ror__(self, other):
        return self.apply("or", other, self)
    def __xor__(self, other):
        return self.apply("xor", self, other)
    def __rxor__(self, other):
        return self.apply("xor", other, self)
    def __lshift__(self, other):
        return self.apply("lshift", self, other)
    def __rshift__(self, other):
        return self.apply("rshift", self, other)

    # logical
    def __eq__(self, other):
        return self.apply("eq", self, other)
    def __ne__(self, other):
        return self.apply("ne", self, other)
    def __lt__(self, other):
        return self.apply("lt", self, other)
    def __le__(self, other):
        return self.apply("le", self, other)
    def __gt__(self, other):
        return self.apply("gt", self, other)
    def __ge__(self, other):
        return self.apply("ge", self, other)
zjm1126
  • 63,397
  • 81
  • 173
  • 221
  • possible duplicate of [Special (magic) methods in Python](http://stackoverflow.com/questions/1090620/special-magic-methods-in-python) – senderle Mar 04 '12 at 04:56

3 Answers3

3

Section 3.4 of the Python Language Reference covers the magic methods.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

See the Special Method Names section in the reference manual, including Basic Customization and Emulating Numeric Types.

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
1

__mod__ is called for %, not for / as you state:

>>> class x(int):
...   def __mod__(self, y):
...     print '__mod__(%s, %s)' % (self, y)
...     return int.__mod__(self, y)
... 
>>> a = x(23)
>>> a / 4
5
>>> a % 4
__mod__(23, 4)
3
>>> 

Make and use similar toy classes to clarify any doubt you may have about a special method, if any are left after you read the docs.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395