From f47c3dea4702a896f2d42c8e418879857c702b1c Mon Sep 17 00:00:00 2001 From: Windson yang Date: Thu, 20 Dec 2018 21:58:50 +0800 Subject: [PATCH 1/2] bpo-35105: Document that CPython accepts invalid identifiers --- Doc/library/functions.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index e7b98eb44b2e21..466d8a161d1381 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -1388,6 +1388,17 @@ 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``. +.. note:: + + setattr() attempts to update the object with the given attr/value pair. + Whether this succeeds and what its affect is is determined by the target object. + If an object's class defines `__slots__`, the attribute may not be writeable. + If an object's class defines property with a setter method, the *setattr()* + will trigger the setter method which may or may not actually write the attribute. + For objects that have a regular dictionary (which is the typical case), the + *setattr()* call can make any string keyed update allowed by the dictionary + including keys that aren't valid identifiers -- for example setattr(a, '1', 'one') + will be the equivalent of vars()['1'] ='one'. .. class:: slice(stop) slice(start, stop[, step]) From 718589703d72b96ae2789702f9108f1d35bfbfa0 Mon Sep 17 00:00:00 2001 From: Windsooon Date: Thu, 12 Sep 2019 07:51:18 +0800 Subject: [PATCH 2/2] fix docs format depend on review --- Doc/library/functions.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index 466d8a161d1381..3f84aeda9c8f9d 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -1391,14 +1391,14 @@ are always available. They are listed here in alphabetical order. .. note:: setattr() attempts to update the object with the given attr/value pair. - Whether this succeeds and what its affect is is determined by the target object. + Whether this succeeds and the effect it has determined by the target object. If an object's class defines `__slots__`, the attribute may not be writeable. - If an object's class defines property with a setter method, the *setattr()* + If an object's class defines :class:`property` with a setter method, the *setattr()* will trigger the setter method which may or may not actually write the attribute. For objects that have a regular dictionary (which is the typical case), the *setattr()* call can make any string keyed update allowed by the dictionary - including keys that aren't valid identifiers -- for example setattr(a, '1', 'one') - will be the equivalent of vars()['1'] ='one'. + including keys that aren't valid identifiers -- for example ``setattr(a, '1', 'one')`` + will be the equivalent of ``vars()['1'] ='one'``. .. class:: slice(stop) slice(start, stop[, step])