The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Python Objects, values and types (data abstraction) - What are objects in Python?
By Guest on 4th November 2022 07:48:41 AM | Syntax: PYTHON | Views: 224



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1.  
  2. 3.1. Objects, values and types
  3. ==============================
  4.  
  5. *Objects* are Python's abstraction for data.  All data in a Python
  6. program is represented by objects or by relations between objects. (In
  7. a sense, and in conformance to Von Neumann's model of a "stored
  8. program computer", code is also represented by objects.)
  9.  
  10. Every object has an identity, a type and a value.  An object's
  11. *identity* never changes once it has been created; you may think of it
  12. as the object's address in memory.  The '"is"' operator compares the
  13. identity of two objects; the "id()" function returns an integer
  14. representing its identity.
  15.  
  16. **CPython implementation detail:** For CPython, "id(x)" is the memory
  17. address where "x" is stored.
  18.  
  19. An object's type determines the operations that the object supports
  20. (e.g., "does it have a length?") and also defines the possible values
  21. for objects of that type.  The "type()" function returns an object's
  22. type (which is an object itself).  Like its identity, an object's
  23. *type* is also unchangeable. [1]
  24.  
  25. The *value* of some objects can change.  Objects whose value can
  26. change are said to be *mutable*; objects whose value is unchangeable
  27. once they are created are called *immutable*. (The value of an
  28. immutable container object that contains a reference to a mutable
  29. object can change when the latter's value is changed; however the
  30. container is still considered immutable, because the collection of
  31. objects it contains cannot be changed.  So, immutability is not
  32. strictly the same as having an unchangeable value, it is more subtle.)
  33. An object's mutability is determined by its type; for instance,
  34. numbers, strings and tuples are immutable, while dictionaries and
  35. lists are mutable.
  36.  
  37. Objects are never explicitly destroyed; however, when they become
  38. unreachable they may be garbage-collected.  An implementation is
  39. allowed to postpone garbage collection or omit it altogether --- it is
  40. a matter of implementation quality how garbage collection is
  41. implemented, as long as no objects are collected that are still
  42. reachable.
  43.  
  44. **CPython implementation detail:** CPython currently uses a reference-
  45. counting scheme with (optional) delayed detection of cyclically linked
  46. garbage, which collects most objects as soon as they become
  47. unreachable, but is not guaranteed to collect garbage containing
  48. circular references.  See the documentation of the "gc" module for
  49. information on controlling the collection of cyclic garbage. Other
  50. implementations act differently and CPython may change. Do not depend
  51. on immediate finalization of objects when they become unreachable (so
  52. you should always close files explicitly).
  53.  
  54. Note that the use of the implementation's tracing or debugging
  55. facilities may keep objects alive that would normally be collectable.
  56. Also note that catching an exception with a '"try"..."except"'
  57. statement may keep objects alive.
  58.  
  59. Some objects contain references to "external" resources such as open
  60. files or windows.  It is understood that these resources are freed
  61. when the object is garbage-collected, but since garbage collection is
  62. not guaranteed to happen, such objects also provide an explicit way to
  63. release the external resource, usually a "close()" method. Programs
  64. are strongly recommended to explicitly close such objects.  The
  65. '"try"..."finally"' statement and the '"with"' statement provide
  66. convenient ways to do this.
  67.  
  68. Some objects contain references to other objects; these are called
  69. *containers*. Examples of containers are tuples, lists and
  70. dictionaries.  The references are part of a container's value.  In
  71. most cases, when we talk about the value of a container, we imply the
  72. values, not the identities of the contained objects; however, when we
  73. talk about the mutability of a container, only the identities of the
  74. immediately contained objects are implied.  So, if an immutable
  75. container (like a tuple) contains a reference to a mutable object, its
  76. value changes if that mutable object is changed.
  77.  
  78. Types affect almost all aspects of object behavior.  Even the
  79. importance of object identity is affected in some sense: for immutable
  80. types, operations that compute new values may actually return a
  81. reference to any existing object with the same type and value, while
  82. for mutable objects this is not allowed.  E.g., after "a = 1; b = 1",
  83. "a" and "b" may or may not refer to the same object with the value
  84. one, depending on the implementation, but after "c = []; d = []", "c"
  85. and "d" are guaranteed to refer to two different, unique, newly
  86. created empty lists. (Note that "c = d = []" assigns the same object
  87. to both "c" and "d".)
  88.  
  89. Questions:
  90. What are objects in Python?
  91. What is an example of an object in Python?
  92. How do Python objects work?
  93. What is Python object-oriented programming?
  94. Is OOP in Python hard?
  95.  
  96. Python Object Oriented Programming (OOP) - For Beginners
















Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.

Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.

See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license
  • Recent Pastes