我经常听说这些方法(Object.hashCode和System.identityHashCode)返回对象的地址,或者从地址快速计算的东西; 但我也很确定垃圾收集器会移动并压缩物体.由于哈希码不能改变,这就出现了问题.我知道这不是人们日常工作需要知道的事情,但我想了解内部情况.那么,有谁知道这是如何在Java中实现的?或.NET,因为它们可能类似.
当该p函数用于打印出一个对象时,它可能会给出一个ID,它与object_id()给出的不同.不同数字的原因是什么?
更新: 0x4684abc不同于36971870,是0x234255E
>> a = Point.new
=> #<Point:0x4684abc>
>> a.object_id
=> 36971870
>> a.__id__
=> 36971870
>> "%X" % a.object_id
=> "234255E"
我开始学习Python(python 3.3),我正在尝试is运算符.我试过这个:
>>> b = 'is it the space?'
>>> a = 'is it the space?'
>>> a is b
False
>>> c = 'isitthespace'
>>> d = 'isitthespace'
>>> c is d
True
>>> e = 'isitthespace?'
>>> f = 'isitthespace?'
>>> e is f
False
似乎空间和问号使is行为表现不同.这是怎么回事?
编辑:我知道我应该使用==,我只是想知道为什么is会这样.
如果我有一个比较等于Python集合的元素但不是同一个对象的对象,是否有合理的方法来获取对集合中对象的引用?用例将使用该集来识别和共享重复数据.
示例(Python 2.7):
>>> a = "This is a string"
>>> b = "This is a string"
>>> a is b
False
>>> a == b
True
>>> s = set((a,))
>>> b in s
True
如何获得a使用b和s?我可以想到一种方法,但我不确定它是否与实现无关,无论你是否得到a或b.编辑:当s有多个元素时,这不起作用; 交集很自然地实现了类似的东西[x for x in smaller_set if x in larger_set]
>>> for x in set((b,)).intersection(s): c = x
...
>>> c is a
True
也许一个好的解决方法是使用将每个键映射到自身的字典而不是集合.
以下内容对我有意义:
>>> [] is []
False
鉴于列表是可变的,我希望[]每次它出现在表达式中时都是一个新的空列表对象.但是,使用这个解释,以下让我感到惊讶:
id([]) == id([])
True
为什么?解释是什么?
>>> import sys
>>> sys.version
'2.7.3 (default, Mar 13 2014, 11:03:55) \n[GCC 4.7.2]'
>>> import os
>>> os.remove is os.unlink
False
>>> os.remove == os.unlink
True
这是为什么?是不是os.unlink应该是os.remove的别名?
我注意到对象的ID是以违反直觉的方式分配的.创建对象越早,其对象ID就越大.我原以为他们会按升序分配,而不是相反.
例如:
obj1 = Object.new
obj2 = Object.new
obj3 = Object.new
p obj1.object_id # => 4806560
p obj2.object_id # => 4806540
p obj3.object_id # => 4806520
为什么以这种方式分配它们以及为什么在Ruby解释器运行的代码中有20步而不是1步,但Ruby的irb运行的代码的对象ID之间的区别大得多?
I'm playing around with [[no_unique_address]] in c++20.
In the example on cppreference we have an empty type Empty and type Z
struct Empty {}; // empty class
struct Z {
    char c;
    [[no_unique_address]] Empty e1, e2;
};
Apparently, the size of Z has to be at least 2 because types of e1 and e2 are the same. 
However, I really want to have Z with size 1. This got me thinking, what about wrapping Empty in some wrapper …
t = (1,2,3)
t1 = (1,2,3)
print(id(t))
print(id(t1))
上述代码行在 Python 中的脚本模式下给出了相同的地址,但在交互模式下它输出了不同的地址。任何人都可以解释原因吗?
我想构建一个哈希图,其中键是引用。我希望这些引用的相等意味着引用相等,即两个引用借用同一个对象。
use std::collections::hash_map::HashMap;
struct SomeKey();
struct SomeValue();
fn main() {
    let m = HashMap::<&SomeKey, SomeValue>::new();
    
    let t = SomeKey();
    m.get(&t);
}
不幸的是,这失败了,编译器告诉我&SomeKey没有实现Hash/ Eq。
error[E0599]: the method `get` exists for struct `HashMap<&SomeKey, SomeValue>`, but its trait bounds were not satisfied
  --> src/main.rs:10:7
   |
10 |     m.get(&t);
   |       ^^^ method cannot be called on `HashMap<&SomeKey, SomeValue>` due to unsatisfied trait bounds
   |
   = note: the following trait bounds were not satisfied:
           `&SomeKey: Eq`
           `&SomeKey: Hash`
我注意到,如果我实现 …
object-identity ×10
python ×5
ruby ×2
c++ ×1
c++20 ×1
cpython ×1
empty-list ×1
hashcode ×1
internals ×1
irb ×1
java ×1
list ×1
object ×1
operators ×1
python-2.7 ×1
python-3.x ×1
rust ×1
set ×1
struct ×1
tuples ×1