On 4/26/2012 1:48 AM, John Nagle wrote:

This assumes that everything is, internally, an object.  In CPython,
that's the case, because Python is a naive interpreter and everything,
including numbers, is "boxed". That's not true of PyPy or Shed Skin.
So does "is" have to force the creation of a temporary boxed object?

Python Language Reference
"3.1. Objects, values and types
Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects. ... Every object has an identity, a type and a value. An object’s identity never changes once it has been created; ... The ‘is‘ operator compares the identity of two objects; the id()function* returns an integer representing its identity#." [notes added]

* the built-in function bound to 'id' on startup.
# and that integer must not change for the lifetime of the object.

None of the above is CPython implementation detail.

What the spec 'forces' is observable behavior. Internal shortcuts are allowed. If an interpreter 'knows' that 'a' represents anything other than None, than it can evaluate 'a is None' as False without boxing 'a' anymore than it is already.

The object model above allows for an object to represent or consist of a collection of raw, non-object internal data fields. A list is a sequence of Python objects. A string is not; it is a sequence of 'characters' or 'bytes'. Structs and arrays, including numpy arrays, are similar in containing non-object values. However, when single characters, bytes, or other binary values are extracted and exposed to Python code by indexing or iteration, they must be objectivized (or at least are in CPython).

Worrying about 'is' and 'id' forcing objectness is misplaced. Except for comparing an object to a pre-defined constant or sentinel, 'is' is mainly used for introspection and testing. The main use of 'id()' is to make unambiguous string representations of functions, classes, and modules.

The real 'culprits' for (potentially) forcing objectness are everyday indexing and iteration. Numpy avoids boxing internal binary values by providing functions that operate on numpy arrays *without* exposing the internal values at the Python level. I believe that psycho and now pypy can analyze a function to determine whether machine ints and floats can used without being boxed,

--
Terry Jan Reedy


--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to