From 57522c9b6e5d09aba8a098e704376e3c78d6533c Mon Sep 17 00:00:00 2001 From: Jeff Allen Date: Wed, 31 Aug 2022 08:42:54 +0100 Subject: [PATCH 1/2] Docs: attributes need not be identifiers Clarify that the name of an attribute need not be a valid Python identifier, in particular through setarr(), noting that this precludes use of the dot notation to access them. --- Doc/glossary.rst | 10 ++++++++-- Doc/library/functions.rst | 8 ++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Doc/glossary.rst b/Doc/glossary.rst index e0dd4fc96760e7..5813c7820a00b5 100644 --- a/Doc/glossary.rst +++ b/Doc/glossary.rst @@ -136,10 +136,16 @@ Glossary :exc:`StopAsyncIteration` exception. Introduced by :pep:`492`. attribute - A value associated with an object which is referenced by name using - dotted expressions. For example, if an object *o* has an attribute + A value associated with an object which is usually referenced by name + using dotted expressions. + For example, if an object *o* has an attribute *a* it would be referenced as *o.a*. + It is possible to give an object an attribute whose name is not an + identifier as defined by :ref:`identifiers`, for example using + :func:`setattr`, if the object allows it. + Such an attribute will not be accessible using a dotted expression. + awaitable An object that can be used in an :keyword:`await` expression. Can be a :term:`coroutine` or an object with an :meth:`__await__` method. diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index e86e1857c7a67d..c0fa707f968bd3 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -397,6 +397,7 @@ are always available. They are listed here in alphabetical order. string. The string must be the name of one of the object's attributes. The function deletes the named attribute, provided the object allows it. For example, ``delattr(x, 'foobar')`` is equivalent to ``del x.foobar``. + *name* need not be a Python identifier (see :func:`setattr`). .. _func-dict: @@ -738,6 +739,7 @@ are always available. They are listed here in alphabetical order. value of that attribute. For example, ``getattr(x, 'foobar')`` is equivalent to ``x.foobar``. If the named attribute does not exist, *default* is returned if provided, otherwise :exc:`AttributeError` is raised. + *name* need not be a Python identifier (see :func:`setattr`). .. note:: @@ -1575,6 +1577,12 @@ are always available. They are listed here in alphabetical order. object allows it. For example, ``setattr(x, 'foobar', 123)`` is equivalent to ``x.foobar = 123``. + *name* need not be a Python identifier as defined in :ref:`identifiers` + unless the object chooses to enforce that, for example in a custom + :meth:`~object.__getattribute__` or via :attr:`~object.__slots__`. + An attribute whose name is not an identifier will not be accessible using + the dot notation, but is accessible through :func:`getattr` etc.. + .. note:: Since :ref:`private name mangling ` happens at From 6deda0413cfb0c365c58e88917febb682e028830 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Thu, 29 Sep 2022 14:51:21 -0700 Subject: [PATCH 2/2] Mention getattr in definition of attribute Co-authored-by: C.A.M. Gerlach --- Doc/glossary.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/glossary.rst b/Doc/glossary.rst index 5813c7820a00b5..9385b8ddd13d10 100644 --- a/Doc/glossary.rst +++ b/Doc/glossary.rst @@ -144,7 +144,8 @@ Glossary It is possible to give an object an attribute whose name is not an identifier as defined by :ref:`identifiers`, for example using :func:`setattr`, if the object allows it. - Such an attribute will not be accessible using a dotted expression. + Such an attribute will not be accessible using a dotted expression, + and would instead need to be retrieved with :func:`getattr`. awaitable An object that can be used in an :keyword:`await` expression. Can be