(In regards to Python 3.2)
I'm trying to make a statement along the lines of:
In Python, an object is...
According to the doc (http://docs.python.org/py3k/reference/datamodel.html#objects-values-and-types):
Every object has an identity, a type and a value
But where do attributes fall into that? If I do something like a = 3; print(a.__class__)
I get <class 'int'>
I assume that is the type of the object a
references, meaning that "type" is an "attribute" of an object. So in that sense we can say a sufficient set of "things" an object has would be its identity, value and attributes. However, looking through the attributes of a using dir(a)
, I do not see anything resembling identity (even though I know the id()
function will tell me that information).
So my question is are any of the following minimal statements to sufficiently describe the notion of a Python object?
In Python an object has attributes, of which always include an identity, type and value.
In Python an object has an identity and attributes, of which always include its type and value.
In Python an object has an identity, value and attributes, of which always include its type, among other things.
If not, could someone give me a definition that conveys the relationships attributes, identity, type and value for an object?
(I would prefer number 1 to be true. :P)
While you can access the type of an object through an attribute, its type isn't just an attribute -- the type defines how the object was created before it had any attributes at all. By that fact alone none of those statements is sufficient to describe a Python object.
I'd say it this way:
In Python, everything is an object.
An object is a block of information, which has a type, which defines its creation and how it interacts with other objects, an identity, which differentiates it from all other objects, and a value, which is the information in the block. Attributes are other objects associated with a given object, including the object that is its type.
You should then give some examples of things people might not expect to be objects, like functions.
A paragraph on "What is an object" can be found in Dive Into Python:
Everything in Python is an object, and almost everything has attributes and methods. All functions have a built-in attribute
__doc__
, which returns the doc string defined in the function's source code. Thesys
module is an object which has (among other things) an attribute calledpath
. And so forth.Still, this begs the question. What is an object? Different programming languages define “object” in different ways. In some, it means that all objects must have attributes and methods; in others, it means that all objects are subclassable. In Python, the definition is looser; some objects have neither attributes nor methods (more on this in Chapter 3), and not all objects are subclassable (more on this in Chapter 5). But everything is an object in the sense that it can be assigned to a variable or passed as an argument to a function (more in this in Chapter 4).
I think you are getting your terminology mixed up.
The identity of an object is just a value that uniquely identifies that object during it's lifetime. So, id(1)
is guaranteed to be different from id(2)
.
The type of an object tells you something more; it tells you what operations you can use with that object, and what possible values can be stored in that object. There are two kinds of types: built-in and user-defined. Built-in types are int
, string
, etc. User-defined are the classes you define yourself.
This is what is stored inside the type.
These are additional variables that you can get to from your existing object.
>>> s = "foo" # s is a new variable
>>> id(s) # it has a unique identifier
4299579536
>>> type(s) # it has a type ("str")
<type 'str'>
>>> s # it has a value ("foo")
'foo'
>>> s.__class__ # it has an attribute called __class__ that has the following value:
<type 'str'>
So all three of your statements could be correct, but #3 sounds "most correct" to me. I have also heard
In Python, an object is a dict.
Which makes sense to me, but might not make sense to everyone.
But where do attributes fall into that?
(Some combination of) a particular object's attributes record and determine its value.
All three of value, type and identity are abstract concepts that can only really be understood by example. Consider:
>>> a = (1, 2)
>>> b = (1, 2)
>>> c = a
>>> d = (3, 4)
>>> e = 2
You no doubt understand that a
, b
, and c
are all equal, but only a and c are the same object. That is, all three have the same value, and a and c have the same identity. d
is the same type of object as a
(a tuple), but has a different value; e is a completely different type (an int).
The value of e
is trivial to understand for a human (since int literals are spelled with well known symbols), but Python keeps track of several attributes for it:
>>> e.numerator
2
>>> e.denominator
1
>>> e.real
2
>>> e.imag
0
These together determine the value - although being an int
, it is guaranteed that the denominator is 1, and the imaginary part is 0 (and, indeed, all built in Rationals have a zero imaginary part). So, the value of an int
is its numerator.
But "value" is something abstract - it makes sense for humans, the computer has to distil it down a bit more. Similarly with type - "what type of thing is this?" is something humans understand directly; computers don't, but Python uses classes to implement it - hence,
>>> type('')
<class 'str'>
>>> type(2)
<class 'int'>
You ask "what type of thing is this?", and it gives you back its class. The type is stored as an attribute, but only because Python considers an object's type to be (a potential) part of its value. Identity isn't part of the value (although in some cases, they can be equivalent), and so it isn't recorded in an attribute - its stored in the internal machinery that lets Python find the object you want when you say a
.