Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type attribute hides member of same name #35480

Closed
mclay mannequin opened this issue Nov 6, 2001 · 4 comments
Closed

type attribute hides member of same name #35480

mclay mannequin opened this issue Nov 6, 2001 · 4 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@mclay
Copy link
Mannequin

mclay mannequin commented Nov 6, 2001

BPO 478768
Nosy @gvanrossum, @tim-one
Files
  • hideMemberError.patch: type attribute hides member of same name
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/gvanrossum'
    closed_at = <Date 2001-11-28.03:02:10.000>
    created_at = <Date 2001-11-06.16:53:35.000>
    labels = ['interpreter-core']
    title = 'type attribute hides member of same name'
    updated_at = <Date 2001-11-28.03:02:10.000>
    user = 'https://bugs.python.org/mclay'

    bugs.python.org fields:

    activity = <Date 2001-11-28.03:02:10.000>
    actor = 'gvanrossum'
    assignee = 'gvanrossum'
    closed = True
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2001-11-06.16:53:35.000>
    creator = 'mclay'
    dependencies = []
    files = ['192']
    hgrepos = []
    issue_num = 478768
    keywords = []
    message_count = 4.0
    messages = ['7443', '7444', '7445', '7446']
    nosy_count = 3.0
    nosy_names = ['gvanrossum', 'tim.peters', 'mclay']
    pr_nums = []
    priority = 'normal'
    resolution = 'rejected'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue478768'
    versions = ['Python 2.2']

    @mclay
    Copy link
    Mannequin Author

    mclay mannequin commented Nov 6, 2001

    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
    >>>

    @mclay mclay mannequin closed this as completed Nov 6, 2001
    @mclay mclay mannequin assigned gvanrossum Nov 6, 2001
    @mclay mclay mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 6, 2001
    @mclay mclay mannequin closed this as completed Nov 6, 2001
    @mclay mclay mannequin assigned gvanrossum Nov 6, 2001
    @mclay mclay mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 6, 2001
    @tim-one
    Copy link
    Member

    tim-one commented Nov 6, 2001

    Logged In: YES
    user_id=31435

    Changed category, and assigned to me in Guido's absence.

    @tim-one
    Copy link
    Member

    tim-one commented Nov 27, 2001

    Logged In: YES
    user_id=31435

    Reassigned to Guido.

    @gvanrossum
    Copy link
    Member

    Logged In: YES
    user_id=6380

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

    __slots__ needs documentation.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants