为什么自我没有传递给id()? [英] why is self not passed to id()?

查看:77
本文介绍了为什么自我没有传递给id()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



您好!


执行以下小程序会给我一个TypeError。


是什么让我惊叹是的,foo确实得到了一个参数,而酒吧

不是。谁能解释为什么??????


谢谢

Ruediger


class foo(list):

__hash__ = lambda x:id(x)


班级栏(列表):

__hash__ = id


_s_ = set()

_s_.add(foo())

_s_.add(bar())
< br $> b $ b rue @ linux:~python test01.py

回溯(最近一次调用最后一次):

文件test01.py,第9行,在< module>

_s_.add(bar())

TypeError:id()只取一个参数(0给定)

解决方案

9月4日,3:26 * pm,Ruediger< larud ... @ freenet.dewrote:


你好!


执行以下小程序会给我一个TypeError。


让我惊讶的是foo确实得到了一个论点通过了吧

不是。谁能解释为什么??????


谢谢

Ruediger


class foo(list):

* * __hash__ = lambda x:id(x)


班级栏(列表):

* * __hash__ = id


_s_ = set()

_s_.add(foo())

_s_.add(bar())


rue @ linux:~python test01.py

Traceback(最近一次调用最后一次):

*文件" test01。 py",第9行,在< module>

* * _s_.add(bar())

TypeError:id()只取一个参数(给定0)



答案是相当技术性的。要将成员函数绑定到

实例,它们必须具有__get__方法(需要

实例和所有者作为参数)。 ''id''没有。


(为什么'id''没有__get__方法?)


相比之下,


>> set.add



< method''add''''set''对象>


>> dir(_)



[''__call__'',''__ class__'',''__ delattr __'','__ _doc__'',''__ get__'',

''__ getattribute__

'',''__ hash__'',''__ init__'',''_ _ 1 _''''''__ new__'',''__ objclass __'',
''__ reduce__'',

''__ reduce_ex __'','_ _ _ _ _ _ _ _'''''''''__ setattr _'''''''_ _ _ _ _ _ _ _ _''''

>
''set.add''确实。


Ruediger写道:


执行以下小程序给了我一个TypeError。


是什么让我想知道foo确实得到了一个参数,而bar

不是。任何人都可以解释为什么??????


>> id



<内置函数id>


>> lambda x:id(x)



< function< lambdaat 0x00C07C30>


你没有使用Python写的任何特殊原因Python程序,

btw?


< / F>


castironpi写道:


>

答案是相当技术性的。要将成员函数绑定到

实例,它们必须具有__get__方法(需要

实例和所有者作为参数)。 ''id''没有。


(为什么'id''没有__get__方法?)


相比之下,


>>> set.add



< method''添加''''set''对象>


>>> dir(_)



[''__call__'',''__ class__'',''__ delattr__'''''__ doc__'''''__ get__'',

''__ getattribute__

'',''__ hash__'',''__ init _'',''_ _ _ _ _'''''''__ ___''''''__ objclass__'',

''__ reduce__'' ,

''__ reduce_ex__'',''_ _ _ _ _ _ _ _''''''''''__ setattr _'''''''_ _ _ _ _ _ _ _'''


''set.add ''的确如此。



感谢您的快速回复。


然而,这让我对我心中的小小性能破解的希望减少了

将继续有效。



Hello!

Executing following little program gives me an TypeError.

What makes me wonder is that foo does get an argument passed while bar
doesn''t. Can anyone explain why??????

Thanks
Ruediger

class foo(list):
__hash__ = lambda x: id(x)

class bar(list):
__hash__ = id

_s_ = set()
_s_.add(foo())
_s_.add(bar())

rue@linux:~python test01.py
Traceback (most recent call last):
File "test01.py", line 9, in <module>
_s_.add(bar())
TypeError: id() takes exactly one argument (0 given)

解决方案

On Sep 4, 3:26*pm, Ruediger <larud...@freenet.dewrote:

Hello!

Executing following little program gives me an TypeError.

What makes me wonder is that foo does get an argument passed while bar
doesn''t. Can anyone explain why??????

Thanks
Ruediger

class foo(list):
* * __hash__ = lambda x: id(x)

class bar(list):
* * __hash__ = id

_s_ = set()
_s_.add(foo())
_s_.add(bar())

rue@linux:~python test01.py
Traceback (most recent call last):
* File "test01.py", line 9, in <module>
* * _s_.add(bar())
TypeError: id() takes exactly one argument (0 given)

The answer is fairly technical. For member functions to be bound to
instances, they are required to have a __get__ method (which takes
instance and owner as parameters). ''id'' does not.

(Why does ''id'' not have a __get__ method?)

By contrast,

>>set.add

<method ''add'' of ''set'' objects>

>>dir(_)

[''__call__'', ''__class__'', ''__delattr__'', ''__doc__'', ''__get__'',
''__getattribute__
'', ''__hash__'', ''__init__'', ''__name__'', ''__new__'', ''__objclass__'',
''__reduce__'',
''__reduce_ex__'', ''__repr__'', ''__setattr__'', ''__str__'']

''set.add'' does.


Ruediger wrote:

Executing following little program gives me an TypeError.

What makes me wonder is that foo does get an argument passed while bar
doesn''t. Can anyone explain why??????

>>id

<built-in function id>

>>lambda x: id(x)

<function <lambdaat 0x00C07C30>

any special reason why you''re not using Python to write Python programs,
btw?

</F>


castironpi wrote:

>
The answer is fairly technical. For member functions to be bound to
instances, they are required to have a __get__ method (which takes
instance and owner as parameters). ''id'' does not.

(Why does ''id'' not have a __get__ method?)

By contrast,

>>>set.add

<method ''add'' of ''set'' objects>

>>>dir(_)

[''__call__'', ''__class__'', ''__delattr__'', ''__doc__'', ''__get__'',
''__getattribute__
'', ''__hash__'', ''__init__'', ''__name__'', ''__new__'', ''__objclass__'',
''__reduce__'',
''__reduce_ex__'', ''__repr__'', ''__setattr__'', ''__str__'']

''set.add'' does.

Thank you for the quick response.

However it gives me less hope that the little performance hack I had in mind
will ever work.


这篇关于为什么自我没有传递给id()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆