This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: type attribute hides member of same name
Type: Stage:
Components: Interpreter Core Versions: Python 2.2
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: gvanrossum, mclay, tim.peters
Priority: normal Keywords:

Created on 2001-11-06 16:53 by mclay, last changed 2022-04-10 16:04 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
hideMemberError.patch mclay, 2001-11-06 16:53 type attribute hides member of same name
Messages (4)
msg7443 - (view) Author: Michael McLay (mclay) Date: 2001-11-06 16:53
Allowing type attributes to replace member descriptors
of the same name removes access to all instance data of
the same name throughout the application. The error
generated is not obvious and may not occur near the
source of the error.  A patch to prevent this is attached. 

The use of __slots__ to define attributes allowed in an
instance of a type eliminates the __dict__ in instance.
Instead the names of the instance attributes are
declared in the __slots__ attribute of the class
definition. The names defined in __slots__ become
member descriptor in the type dict. 

This change has useful advantages, but there is one
side effect that makes the use of __slots__ different
from the semantics of the old style dynamic classes. It
is not possible to have a type attribute and an
instance attribute with the same name.  This eliminates
the idiom of using a class attribute as a default and
then overriding the default value by creating an
instance attribute of the same name. This is no longer
possible because the declaration of a type attribute
name will replace the definition of the member name in
the type dict.  Once this occurs it impossible to
access any instances of the attribute of the same name
as a type attribute because the set and get methods for
accessing the data are held in the member descriptor.

The AttributeError raised in the following example
illustrates the problem with using the names declared
in __slots__ when naming a type attribute.  

>>> class B(object):
...   a = 3
...   __slots__ = ['a']
...
>>> b = B()
>>> b.a = 4
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'B' object attribute 'a' is read-only
>>>

The member descriptor 'a' defined in __slots__ is
inaccessible because the type dict has overwritten the
member descriptor with the type attribute of the same
name. A more descriptive error message can be generated
by checking that no __slots__ names are in the type
dict when the __slots__ are being created by type_new(). 

An instance member defined by __slots__ can also be
hidden by a type attribute following the completion of
the definition of the class by making an assignment to
the type with the same name as the instance member. In
the following example the "B.a = "hiding b.a" replaces
the reference to the member descriptor for instance
member 'a' in the type dict.  This eliminates access to
all instance members of that name throughout the
application.

>>> class B(object):
...   __slots__ = ['a']
...
>>> b = B()
>>> b.a = 4
>>> B.a = "hiding b.a"
>>> b.a
'hiding b.a'
>>> b.a = 5
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'B' object attribute 'a' is read-only
>>>


msg7444 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2001-11-06 17:43
Logged In: YES 
user_id=31435

Changed category, and assigned to me in Guido's absence.
msg7445 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2001-11-27 21:35
Logged In: YES 
user_id=31435

Reassigned to Guido.
msg7446 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2001-11-28 03:02
Logged In: YES 
user_id=6380

Rejected. The patch would make it impossible to replace a
descriptor.

__slots__ needs documentation.
History
Date User Action Args
2022-04-10 16:04:37adminsetgithub: 35480
2001-11-06 16:53:35mclaycreate