标签: object-identity

如果Java的垃圾收集器移动对象,那么什么是Object.hashCode和System.identityHashCode?

我经常听说这些方法(Object.hashCodeSystem.identityHashCode)返回对象的地址,或者从地址快速计算的东西; 但我也很确定垃圾收集器会移动并压缩物体.由于哈希码不能改变,这就出现了问题.我知道这不是人们日常工作需要知道的事情,但我想了解内部情况.那么,有谁知道这是如何在Java中实现的?或.NET,因为它们可能类似.

java garbage-collection internals hashcode object-identity

35
推荐指数
1
解决办法
2084
查看次数

在Ruby中,为什么inspect()会打印出某种与object_id()不同的对象id?

当该p函数用于打印出一个对象时,它可能会给出一个ID,它与object_id()给出的不同.不同数字的原因是什么?

更新: 0x4684abc不同于36971870,是0x234255E

>> a = Point.new
=> #<Point:0x4684abc>

>> a.object_id
=> 36971870

>> a.__id__
=> 36971870

>> "%X" % a.object_id
=> "234255E"
Run Code Online (Sandbox Code Playgroud)

ruby object object-identity

30
推荐指数
1
解决办法
6702
查看次数

比较字符串和空格时,'is'运算符的行为不同

我开始学习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
Run Code Online (Sandbox Code Playgroud)

似乎空间和问号使is行为表现不同.这是怎么回事?

编辑:我知道我应该使用==,我只是想知道为什么is会这样.

python operators object-identity python-3.x

29
推荐指数
3
解决办法
1908
查看次数

如何使用等效对象访问集合的元素?

如果我有一个比较等于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
Run Code Online (Sandbox Code Playgroud)

如何获得a使用bs?我可以想到一种方法,但我不确定它是否与实现无关,无论你是否得到ab.编辑:当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
Run Code Online (Sandbox Code Playgroud)

也许一个好的解决方法是使用将每个键映射到自身的字典而不是集合.

python set object-identity

16
推荐指数
1
解决办法
7254
查看次数

Python什么时候为空列表创建新的列表对象?

以下内容对我有意义:

>>> [] is []
False
Run Code Online (Sandbox Code Playgroud)

鉴于列表是可变的,我希望[]每次它出现在表达式中时都是一个新的空列表对象.但是,使用这个解释,以下让我感到惊讶:

id([]) == id([])
True
Run Code Online (Sandbox Code Playgroud)

为什么?解释是什么?

python list object-identity empty-list

14
推荐指数
1
解决办法
315
查看次数

在Python(2.7)中,为什么os.remove与os.unlink不相同?

>>> 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
Run Code Online (Sandbox Code Playgroud)

这是为什么?是不是os.unlink应该是os.remove的别名?

python object-identity python-2.7 object-equality

10
推荐指数
1
解决办法
4870
查看次数

为什么Ruby倾向于按降序分配对象ID?

我注意到对象的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
Run Code Online (Sandbox Code Playgroud)

为什么以这种方式分配它们以及为什么在Ruby解释器运行的代码中有20步而不是1步,但Ruby的irb运行的代码的对象ID之间的区别大得多?

ruby irb object-identity

8
推荐指数
1
解决办法
376
查看次数

[[no_unique_address]] and two member values of the same type

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;
};
Run Code Online (Sandbox Code Playgroud)

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 …

c++ struct memory-layout object-identity c++20

7
推荐指数
1
解决办法
121
查看次数

尽管元组是不可变的,但它们以交互模式存储在不同的地址中。为什么?

t = (1,2,3)
t1 = (1,2,3)
print(id(t))
print(id(t1))
Run Code Online (Sandbox Code Playgroud)

上述代码行在 Python 中的脚本模式下给出了相同的地址,但在交互模式下它输出了不同的地址。任何人都可以解释原因吗?

python tuples cpython object-identity

7
推荐指数
1
解决办法
77
查看次数

Rust 引用没有实现 Eq/Hash?如何使用它们作为哈希映射键?

我想构建一个哈希图,其中键是引用。我希望这些引用的相等意味着引用相等,即两个引用借用同一个对象。

use std::collections::hash_map::HashMap;

struct SomeKey();
struct SomeValue();

fn main() {
    let m = HashMap::<&SomeKey, SomeValue>::new();
    
    let t = SomeKey();
    m.get(&t);
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这失败了,编译器告诉我&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`
Run Code Online (Sandbox Code Playgroud)

(操场)

我注意到,如果我实现 …

object-identity rust

6
推荐指数
1
解决办法
1796
查看次数