Skip to content

Commit 9a11ed8

Browse files
jeff5CAM-Gerlach
andauthored
pythongh-96397: Document that attributes need not be identifiers (python#96454)
Co-authored-by: C.A.M. Gerlach <[email protected]>
1 parent 0179a82 commit 9a11ed8

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

Doc/glossary.rst

+9-2
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,17 @@ Glossary
136136
:exc:`StopAsyncIteration` exception. Introduced by :pep:`492`.
137137

138138
attribute
139-
A value associated with an object which is referenced by name using
140-
dotted expressions. For example, if an object *o* has an attribute
139+
A value associated with an object which is usually referenced by name
140+
using dotted expressions.
141+
For example, if an object *o* has an attribute
141142
*a* it would be referenced as *o.a*.
142143

144+
It is possible to give an object an attribute whose name is not an
145+
identifier as defined by :ref:`identifiers`, for example using
146+
:func:`setattr`, if the object allows it.
147+
Such an attribute will not be accessible using a dotted expression,
148+
and would instead need to be retrieved with :func:`getattr`.
149+
143150
awaitable
144151
An object that can be used in an :keyword:`await` expression. Can be
145152
a :term:`coroutine` or an object with an :meth:`__await__` method.

Doc/library/functions.rst

+8
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ are always available. They are listed here in alphabetical order.
397397
string. The string must be the name of one of the object's attributes. The
398398
function deletes the named attribute, provided the object allows it. For
399399
example, ``delattr(x, 'foobar')`` is equivalent to ``del x.foobar``.
400+
*name* need not be a Python identifier (see :func:`setattr`).
400401

401402

402403
.. _func-dict:
@@ -738,6 +739,7 @@ are always available. They are listed here in alphabetical order.
738739
value of that attribute. For example, ``getattr(x, 'foobar')`` is equivalent to
739740
``x.foobar``. If the named attribute does not exist, *default* is returned if
740741
provided, otherwise :exc:`AttributeError` is raised.
742+
*name* need not be a Python identifier (see :func:`setattr`).
741743

742744
.. note::
743745

@@ -1582,6 +1584,12 @@ are always available. They are listed here in alphabetical order.
15821584
object allows it. For example, ``setattr(x, 'foobar', 123)`` is equivalent to
15831585
``x.foobar = 123``.
15841586

1587+
*name* need not be a Python identifier as defined in :ref:`identifiers`
1588+
unless the object chooses to enforce that, for example in a custom
1589+
:meth:`~object.__getattribute__` or via :attr:`~object.__slots__`.
1590+
An attribute whose name is not an identifier will not be accessible using
1591+
the dot notation, but is accessible through :func:`getattr` etc..
1592+
15851593
.. note::
15861594

15871595
Since :ref:`private name mangling <private-name-mangling>` happens at

0 commit comments

Comments
 (0)