The definition of an object in Python
Bengt Richter
bokr at oz.net
Sat Jul 19 11:44:13 EDT 2003
On 19 Jul 2003 01:52:56 -0700, wesc at deirdre.org (Wesley J. Chun) wrote:
[...]
>> Wesley says that every Python object must possess the following
>> three characteristics: 1) an identity, which can be retrieved
>> by the function id(); 2) a type, which can be retrieved by
>> the function type(); and 3) a value.
>
>again, this is also (still) true. (1) the identity is what makes an
>object unique from every other object currently in the interpreter.
^^^^^^^^^
This should perhaps be emphasized. E.g., these results are not guaranteed,
but they can happen:
>>> idvalue = id(123)
>>> idvalue, id(456)
(7952152, 7952152)
>>> idvalue == id(456)
1
I.e., the value of id(x) is not valid beyond the lifetime of x, which is
obviously ok, but mistaken reuse of id-like numbers (e.g., such as you get from
file('somefile').fileno()) is a traditional source of bugs, and I imagine
naive use of id() could lead to similar ones.
>some people view this as a "memory address" altho you wouldn't
>use it as such since Python handles the memory management 4 u.
>you are just guaranteed that any other object in the system will
>NOT have the same id. (2) the type of an object defines what its
>characteristics (i.e., what methods and attributes it has, etc.) are,
>and (3) the value goes without saying.
Re (3): until, perhaps, you start taking about copying or pickling? ;-)
Regards,
Bengt Richter
More information about the Python-list
mailing list