Return to Snippet

Revision: 21865
at December 22, 2009 15:30 by magicrebirth


Initial Code
## Every object in Python has an identity, a type, and a value. The identity points to the object's location in memory. The type describes the representation of the object to Python. The value of the object is simply the data stored inside.

## The following example shows how to access the identity, type, and value of an object programmatically using the id(object), type(object), and variable name, respectively:

>>> l = [1,2,3]
>>> print id(l)
9267480
>>> print type(l)
<type 'list'>
>>> print l
[1, 2, 3]


## After an object is created, the identity and type cannot be changed. If the value can be changed, it is considered a mutable object; if the value cannot be changed, it is considered an immutable object.

## Some objects may also have attributes and methods. 
## Attributes are values associated with the object. Methods are callable functions that perform an operation on the object. 
## Attributes and methods of an object can be accessed using the following dot '.' syntax:

>>> class test(object):
...     def printNum(self):
...         print self.num
...
>>> t = test()
>>> t.num = 4
>>> t.printNum()
4

Initial URL


Initial Description


Initial Title
Python: identity of an object

Initial Tags
object, python

Initial Language
Python