模块是可以清洗的吗?! [英] Modules are hashable?!

查看:119
本文介绍了模块是可以清洗的吗?!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在玩,并注意到模块似乎是可以清洗的。

任何人都可以解释一下,特别是考虑到它们是可变的这一事实吗?


Python 2.3.3(#1,2004年5月7日,10:31:40)

[GCC 3.3.3 20040412(Red Hat Linux 3.3.3-7)] on linux2

输入help,copyright,credit等等。或许可证或有关更多信息。

import sys
hash(sys)
-150589324 sys.x = 42
hash(sys)
-150589324 foo = {sys:''bar''}
foo [sys]



''bar''

解决方案

Hello Leif,

我只是在玩耍,注意到模块似乎是可以清洗的。
有人可以解释一下,特别是考虑到它们是可变的吗?


http://docs.python.org/tut/node11.ht...00000000000000

HTH。

-

------------------------- -----------------------------------------------

Miki Tebeka< mi ********* @ zoran.com>
http://tebeka.spymac.net

儿童和成人之间的唯一区别是玩具的价格


Leif K-Brooks写道:

我刚刚玩,并注意到模块似乎是可以清洗的。
任何人都可以解释这一点,特别是考虑到他们的事实'可变吗?




Python中的大多数对象都是可清除的,也是可变的。如果定义一个

类Foo并创建x = Foo(),则x将是可清除的。但你也可以这样做

x.stuff = 3.所以模块似乎是非常常规的对象,它们通常用ID进行哈希处理 - 这对于任何两个对象。

尝试使用hash(sys)== id(sys)来查看。


列表,元组,字符串,字符串和a很少有其他的东西是奇怪的

他们被他们的内容而不是ID进行哈希/比较。所以你可以

有两个不同的对象,相当于散列

或比较。


由于哈希的方式工作时,哈希值不允许更改

而它是在dict中。 ID永远不会改变,因此即使您更改其内容,常规对象也具有

不变的哈希值。对于特殊的

对象,更改内容会改变哈希值,并且'

不允许。


无论如何,我认为这是它的工作方式:P


- Jason Lai


Leif K-Brooks< eu *****@ecritters.biz>写道:

我只是在玩,并注意到模块似乎是可以清洗的。
任何人都能解释一下,特别是考虑到它们是可变的吗? / blockquote>


如果类型(x)不公开__eq__也不公开__cmp__,任何对象x都是可哈希的。

在这种情况下,x ==的含义对于该对象的y是''x是y'',即

id(x)== id(y),因此具有hash(x)返回id(x)是完全有效的。

如果不影响平等比较,突变不是问题。

Alex


I was just playing around, and noticed that modules seem to be hashable.
Can anyone explain that, especially given the fact that they''re mutable?

Python 2.3.3 (#1, May 7 2004, 10:31:40)
[GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

import sys
hash(sys) -150589324 sys.x = 42
hash(sys) -150589324 foo = {sys:''bar''}
foo[sys]


''bar''

解决方案

Hello Leif,

I was just playing around, and noticed that modules seem to be hashable.
Can anyone explain that, especially given the fact that they''re mutable?


http://docs.python.org/tut/node11.ht...00000000000000

HTH.
--
------------------------------------------------------------------------
Miki Tebeka <mi*********@zoran.com>
http://tebeka.spymac.net
The only difference between children and adults is the price of the toys


Leif K-Brooks wrote:

I was just playing around, and noticed that modules seem to be hashable.
Can anyone explain that, especially given the fact that they''re mutable?



Most objects in Python are hashable, and also mutable. If you define a
class Foo and create x = Foo(), x will be hashable. But you can also do
x.stuff = 3. So modules appear to be pretty much regular objects, which
are usually hashed by ID -- which will be unique for any two objects.
Try hash(sys) == id(sys) to see for yourself.

Lists, tuples, dicts, strings, and a few other things are odd in that
they''re hashed/compared by their contents rather than ID. So you can
have two different objects that are equivalent with regards to hashing
or comparing.

Due to the way hashing works, the hash value is not allowed to change
while it''s in a dict. IDs never change, so regular objects have an
unchanging hash value even if you change their contents. For the special
objects, changing the contents would change the hash value, and that''s
not allowed.

I think that''s how it works, anyway :P

- Jason Lai


Leif K-Brooks <eu*****@ecritters.biz> wrote:

I was just playing around, and noticed that modules seem to be hashable.
Can anyone explain that, especially given the fact that they''re mutable?



Any object x is hashable if type(x) does not expose __eq__ nor __cmp__.
In that case, the meaning of x==y for that object is ''x is y'', that is,
id(x)==id(y), so having hash(x) return id(x) is perfectly functional.
Mutation is not a problem if it doesn''t affect equality comparisons.
Alex


这篇关于模块是可以清洗的吗?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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