Python: identity of an object


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. ## 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.
  2.  
  3. ## 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:
  4.  
  5. >>> l = [1,2,3]
  6. >>> print id(l)
  7. 9267480
  8. >>> print type(l)
  9. <type 'list'>
  10. >>> print l
  11. [1, 2, 3]
  12.  
  13.  
  14. ## 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.
  15.  
  16. ## Some objects may also have attributes and methods.
  17. ## Attributes are values associated with the object. Methods are callable functions that perform an operation on the object.
  18. ## Attributes and methods of an object can be accessed using the following dot '.' syntax:
  19.  
  20. >>> class test(object):
  21. ... def printNum(self):
  22. ... print self.num
  23. ...
  24. >>> t = test()
  25. >>> t.num = 4
  26. >>> t.printNum()
  27. 4

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.