相关疑难解决方法(0)

Python方法包装器类型?

Python 3中的方法包装器类型是什么?如果我这样定义一个类:

class Foo(object):
    def __init__(self, val):
        self.val = val
    def __eq__(self, other):
        return self.val == other.val
Run Code Online (Sandbox Code Playgroud)

然后做:

Foo(42).__eq__
Run Code Online (Sandbox Code Playgroud)

我明白了:

<bound method Foo.__eq__ of <__main__.Foo object at 0x10121d0>>
Run Code Online (Sandbox Code Playgroud)

但如果我这样做(在Python 3中):

Foo(42).__ne__
Run Code Online (Sandbox Code Playgroud)

我明白了:

<method-wrapper '__ne__' of Foo object at 0x1073e50>
Run Code Online (Sandbox Code Playgroud)

什么是"方法包装"类型?

编辑:抱歉更准确:class method-wrapper是类型__ne__,就像我做的那样:

>>> type(Foo(42).__ne__)
<class 'method-wrapper'>
Run Code Online (Sandbox Code Playgroud)

而类型__eq__是:

>>> type(Foo(42).__eq__)
<class 'method'>
Run Code Online (Sandbox Code Playgroud)

此外,method-wrapper似乎是类上任何未定义的魔术方法的类型(因此__le__,如果没有明确定义__repr__,则__str__等也将具有此类型).

我感兴趣的是method-wrapperPython 如何使用该类.类的方法的所有"默认实现"都只是这种类型的实例吗?

types python-3.x

32
推荐指数
2
解决办法
2万
查看次数

什么是python中的'slot wrapper'?

object.__dict__ 和其他地方的隐藏方法设置为这样的事情:

 <dictproxy {'__add__': <slot wrapper '__add__' of 'instance' objects>,
 '__and__': <slot wrapper '__and__' of 'instance' objects>,
 '__call__': <slot wrapper '__call__' of 'instance' objects>,
 '__cmp__': <slot wrapper '__cmp__' of 'instance' objects>,
 '__coerce__': <slot wrapper '__coerce__' of 'instance' objects>,
 '__contains__': <slot wrapper '__contains__' of 'instance' objects>,
 '__delattr__': <slot wrapper '__delattr__' of 'instance' objects>,
 '__delitem__': <slot wrapper '__delitem__' of 'instance' objects>,
 '__delslice__': <slot wrapper '__delslice__' of 'instance' objects>,
 '__div__': <slot wrapper '__div__' of 'instance' objects>,
 '__divmod__': <slot wrapper '__divmod__' of 'instance' objects>, …
Run Code Online (Sandbox Code Playgroud)

python

19
推荐指数
1
解决办法
2593
查看次数

标签 统计

python ×1

python-3.x ×1

types ×1