Skip to content

Nested class __name__ #37422

Closed
Closed

Description

@gvanrossum
BPO 633930
Nosy @gvanrossum, @birkenfeld

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 2012-07-29.19:01:53.589>
created_at = <Date 2002-11-05.17:56:34.000>
labels = ['interpreter-core']
title = 'Nested class __name__'
updated_at = <Date 2012-07-29.19:01:53.589>
user = 'https://github.com/gvanrossum'

bugs.python.org fields:

activity = <Date 2012-07-29.19:01:53.589>
actor = 'mstefanro'
assignee = 'gvanrossum'
closed = True
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2002-11-05.17:56:34.000>
creator = 'gvanrossum'
dependencies = []
files = []
hgrepos = []
issue_num = 633930
keywords = []
message_count = 7.0
messages = ['13078', '13079', '13080', '13081', '13082', '166775', '166802']
nosy_count = 4.0
nosy_names = ['gvanrossum', 'georg.brandl', 'xiaq', 'mstefanro']
pr_nums = []
priority = 'low'
resolution = None
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue633930'
versions = ['Python 2.6', 'Python 2.7']

Activity

  1. gvanrossum commented on Nov 5, 2002

    @gvanrossum
    MemberAuthor

    The __name__ attribute of a nested class should be set
    to 'outer.inner', both for classic and for new-style
    classes. E.g.

    >>> class C:
    ...     class C1: pass
    ... 
    >>> C.C1.__name__
    'C.C1'
    >>>
  2. self-assigned this
    on Nov 5, 2002
  3. self-assigned this
    on Nov 5, 2002
  4. gvanrossum commented on Nov 14, 2002

    @gvanrossum
    MemberAuthor

    Logged In: YES
    user_id=6380

    Hm, but should this also be done for functions inside
    classes? E.g.

    class C:
     def foo(self): pass

    assert C.foo.__name__ == "C.foo"
    assert C.__dict__["foo"].__name__ == "C.foo"

    And what about classes inside functions?

    def f():
      class C: pass
      return C

    assert f().__name__ == "f.C"

    ???

  5. gvanrossum commented on Feb 11, 2003

    @gvanrossum
    MemberAuthor

    Logged In: YES
    user_id=6380

    I'm less sure I even want this now, and not at all sure how
    to do it any more, so lowering priority.

  6. birkenfeld commented on Mar 10, 2007

    @birkenfeld
    Member

    Any interest for Python 3000 on this? Otherwise we can close it.

  7. gvanrossum commented on Mar 10, 2007

    @gvanrossum
    MemberAuthor

    I don't think so. Closing.

  8. xiaq commented on Jul 29, 2012

    xiaqmannequin
    Mannequin

    There is a bigger problem:

    >>> class C:
    ...     class D:
    ...         pass
    ...
    >>> repr(C)
    "<class '__main__.C'>"
    >>> repr(C.D)
    "<class '__main__.D'>"

    Default repr on nested classes produce specious results.

    The problem become pratical when you have two nested classes C1.D and C2.D in module m, which end up being both "m.D" in exception traceback.

    Classes nested in function are likely to contain some information from the function arguments and are thus different on each function call, making it impossible to have a general way to name them. Thus I propose embedding the resulting class's id in name, like what i'm doing manually in a hulking way:

    >>> def factory(foo):
    ...     class C(object):
    ...         bar = foo
    ...     func_name = 'factory'
    ...     C.__name__ = '%s generated classobj at 0x%x' % (func_name, id(C))
    ...     return C
    ...
    >>> factory(0)
    <class '__main__.factory generated classobj at 0x32273c0'>
    >>> factory(0)
    <class '__main__.factory generated classobj at 0x32245b0'>

    Please consider reopening this issue.

  9. mstefanro commented on Jul 29, 2012

    mstefanromannequin
    Mannequin

    Only an issue in Python2.

        >>> A.B.__qualname__
        'A.B'
        >>> repr(A.B)
        "<class '__main__.A.B'>"
  10. transferred this issue fromon Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions