object-identity

When does Python create new list objects for empty lists?

心已入冬 提交于 2019-12-18 03:57:20
问题 The following makes sense to me: >>> [] is [] False Given that lists are mutable, I would expect [] to be a new empty list object every time it appears in an expression. Using this explanation however, the following surprises me: id([]) == id([]) True Why? What is the explanation? 回答1: In the first example, [] is not [] precisely because the lists are mutable. If they weren't, they could safely map to the same one without issue. In the second example, id([]) creates a list, gets the id, and

In Ruby, why does inspect() print out some kind of object id which is different from what object_id() gives?

僤鯓⒐⒋嵵緔 提交于 2019-12-17 17:36:48
问题 When the p function is used to print out an object, it may give an ID, and it is different from what object_id() gives. What is the reason for the different numbers? Update: 0x4684abc is different from 36971870 , which is 0x234255E >> a = Point.new => #<Point:0x4684abc> >> a.object_id => 36971870 >> a.__id__ => 36971870 >> "%X" % a.object_id => "234255E" 回答1: The default implementation of inspect calls the default implementation of to_s , which just shows the hexadecimal value of the object

$all parameter in mongodb does not work with ObjectId list

痴心易碎 提交于 2019-12-12 03:15:44
问题 I retrieve a list of ObjectId and I want to retrieve all object in my mongo database using the parameter $all I'm using pymongo and my request look like this : db.database.collection.find({ "_id" : { "$all" : [ObjectId('4ee371837c93dd33dc000003'),ObjectId('4eef9f647c93dd1a90000000')] } }) but the cursor count returned by the request is 0 but when I do this request: db.database.collection.find_one({ "_id" : ObjectId('4ee371837c93dd33dc000003')}) It returns me the good object Anyone know why it

What objects are guaranteed to have different identity?

我是研究僧i 提交于 2019-12-10 09:54:30
问题 ORIGINAL QUESTION: (My question applies to Python 3.2+, but I doubt this has changed since Python 2.7.) Suppose I use an expression that we usually expect to create an object. Examples: [1,2,3] ; 42 ; 'abc' ; range(10) ; True ; open('readme.txt') ; MyClass() ; lambda x : 2 * x ; etc. Suppose two such expressions are executed at different times and "evaluate to the same value" (i.e., have the same type, and compare as equal). Under what conditions does Python provide what I call a distinct

In Python (2.7), why is os.remove not identical to os.unlink?

有些话、适合烂在心里 提交于 2019-12-09 09:10:47
问题 >>> 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 Why is that? Isn't os.unlink supposed to be an alias of os.remove? 回答1: To answer this question we have to dive a bit into the details of how the python interpreter works. It might be different in other python implementations. First let's start where the os.remove and os.unlink functions are defined. In Modules/posixmodule.c they

Is there a Python equivalent of Java's IdentityHashMap?

浪子不回头ぞ 提交于 2019-12-08 04:53:06
问题 I'm walking a data structure and would like to build a dict mapping X->Y, where X is a field in the data structure I'm walking and Y is a field in the data structure I'm building on the fly. X is an unhashable type. 回答1: Trivially: idmap = {} idmap[id(x)] = y Use the id of x as the dictionary key 回答2: The purpose of Java's IdentityHashMap is to simulate dynamic field. Since Python language already supports dynamic attributes directly, you don't need the map, just assign Y to an X's attribute

What objects are guaranteed to have different identity?

懵懂的女人 提交于 2019-12-06 02:16:18
ORIGINAL QUESTION: (My question applies to Python 3.2+, but I doubt this has changed since Python 2.7.) Suppose I use an expression that we usually expect to create an object. Examples: [1,2,3] ; 42 ; 'abc' ; range(10) ; True ; open('readme.txt') ; MyClass() ; lambda x : 2 * x ; etc. Suppose two such expressions are executed at different times and "evaluate to the same value" (i.e., have the same type, and compare as equal). Under what conditions does Python provide what I call a distinct object guarantee that the two expressions actually create two distinct objects (i.e., x is y evaluates as

If Java's garbage collector moves objects, what is Object.hashCode and System.identityHashCode?

醉酒当歌 提交于 2019-12-04 08:08:15
问题 I've often heard that these methods ( Object.hashCode and System.identityHashCode ) return the address of the object, or something computed quickly from the address; but I'm also pretty sure the garbage collector moves and compacts objects. Since the hash code cannot change, this presents a problem. I know this is not something one needs to know for everyday work, but I'd like to understand the internals. So, does anyone know how this is implemented in Java? Or .NET, since they are probably

If Java's garbage collector moves objects, what is Object.hashCode and System.identityHashCode?

老子叫甜甜 提交于 2019-12-02 20:10:31
I've often heard that these methods ( Object.hashCode and System.identityHashCode ) return the address of the object, or something computed quickly from the address; but I'm also pretty sure the garbage collector moves and compacts objects. Since the hash code cannot change, this presents a problem. I know this is not something one needs to know for everyday work, but I'd like to understand the internals. So, does anyone know how this is implemented in Java? Or .NET, since they are probably similar. Chris Shain .NET's implementation is intentionally not published (and when you attempt to

String concatenation in Python

大城市里の小女人 提交于 2019-12-01 22:10:29
问题 Can you describe difference between two ways of string concatenation: simple __add__ operator and %s patterns? I had some investigation in this question and found %s (in form without using parentheses) a little faster. Also another question was appeared: why result of 'hell%s' % 'o' refers to another memory region than 'hell%s' % ('o',) ? There is some code example: l = ['hello', 'hell' + 'o', 'hell%s' % 'o', 'hell%s' % ('o',)] print [id(s) for s in l] Result: [34375618400, 34375618400,