"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "Doc/faq/programming.rst" between
Python-3.9.3.tar.xz and Python-3.9.4.tar.xz

About: Python is an interpreted, interactive, object-oriented programming language. It incorporates modules, exceptions, dynamic typing, very high level dynamic data types, and classes. Newest major release.

programming.rst  (Python-3.9.3.tar.xz):programming.rst  (Python-3.9.4.tar.xz)
skipping to change at line 1648 skipping to change at line 1648
deleted immediately after execution of the ``id()`` call. To be sure that deleted immediately after execution of the ``id()`` call. To be sure that
objects whose id you want to examine are still alive, create another reference objects whose id you want to examine are still alive, create another reference
to the object: to the object:
>>> a = 1000; b = 2000 >>> a = 1000; b = 2000
>>> id(a) # doctest: +SKIP >>> id(a) # doctest: +SKIP
13901272 13901272
>>> id(b) # doctest: +SKIP >>> id(b) # doctest: +SKIP
13891296 13891296
When can I rely on identity tests with the *is* operator?
---------------------------------------------------------
The ``is`` operator tests for object identity. The test ``a is b`` is
equivalent to ``id(a) == id(b)``.
The most important property of an identity test is that an object is always
identical to itself, ``a is a`` always returns ``True``. Identity tests are
usually faster than equality tests. And unlike equality tests, identity tests
are guaranteed to return a boolean ``True`` or ``False``.
However, identity tests can *only* be substituted for equality tests when
object identity is assured. Generally, there are three circumstances where
identity is guaranteed:
1) Assignments create new names but do not change object identity. After the
assignment ``new = old``, it is guaranteed that ``new is old``.
2) Putting an object in a container that stores object references does not
change object identity. After the list assignment ``s[0] = x``, it is
guaranteed that ``s[0] is x``.
3) If an object is a singleton, it means that only one instance of that object
can exist. After the assignments ``a = None`` and ``b = None``, it is
guaranteed that ``a is b`` because ``None`` is a singleton.
In most other circumstances, identity tests are inadvisable and equality tests
are preferred. In particular, identity tests should not be used to check
constants such as :class:`int` and :class:`str` which aren't guaranteed to be
singletons::
>>> a = 1000
>>> b = 500
>>> c = b + 500
>>> a is c
False
>>> a = 'Python'
>>> b = 'Py'
>>> c = b + 'thon'
>>> a is c
False
Likewise, new instances of mutable containers are never identical::
>>> a = []
>>> b = []
>>> a is b
False
In the standard library code, you will see several common patterns for
correctly using identity tests:
1) As recommended by :pep:`8`, an identity test is the preferred way to check
for ``None``. This reads like plain English in code and avoids confusion with
other objects that may have boolean values that evaluate to false.
2) Detecting optional arguments can be tricky when ``None`` is a valid input
value. In those situations, you can create an singleton sentinel object
guaranteed to be distinct from other objects. For example, here is how
to implement a method that behaves like :meth:`dict.pop`::
_sentinel = object()
def pop(self, key, default=_sentinel):
if key in self:
value = self[key]
del self[key]
return value
if default is _sentinel:
raise KeyError(key)
return default
3) Container implementations sometimes need to augment equality tests with
identity tests. This prevents the code from being confused by objects such as
``float('NaN')`` that are not equal to themselves.
For example, here is the implementation of
:meth:`collections.abc.Sequence.__contains__`::
def __contains__(self, value):
for v in self:
if v is value or v == value:
return True
return False
Modules Modules
======= =======
How do I create a .pyc file? How do I create a .pyc file?
---------------------------- ----------------------------
When a module is imported for the first time (or when the source file has When a module is imported for the first time (or when the source file has
changed since the current compiled file was created) a ``.pyc`` file containing changed since the current compiled file was created) a ``.pyc`` file containing
the compiled code should be created in a ``__pycache__`` subdirectory of the the compiled code should be created in a ``__pycache__`` subdirectory of the
directory containing the ``.py`` file. The ``.pyc`` file will have a directory containing the ``.py`` file. The ``.pyc`` file will have a
 End of changes. 1 change blocks. 
0 lines changed or deleted 86 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)