谁可以告诉我在下一个代码中可以调用内置函数的内容

时间:2009-12-31 00:01:04

标签: python

我知道其中一些,例如

__mod__将致电/

__eq__将致电==>和<

但我不知道所有。

    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)

3 个答案:

答案 0 :(得分:3)

Python语言参考的

Section 3.4涵盖了魔术方法。

答案 1 :(得分:2)

请参阅参考手册中的Special Method Names部分,其中包括Basic CustomizationEmulating Numeric Types

答案 2 :(得分:1)

{p> __mod__代表%代表/,因为您声明:

>>> 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
>>> 

制作并使用类似的玩具类,以澄清您对特殊方法的任何疑问,如果在您阅读the docs之后遗漏了任何疑问。