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

Nested class __name__ #37422

Closed
gvanrossum opened this issue Nov 5, 2002 · 7 comments
Closed

Nested class __name__ #37422

gvanrossum opened this issue Nov 5, 2002 · 7 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@gvanrossum
Copy link
Member

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

@gvanrossum
Copy link
Member Author

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

@gvanrossum gvanrossum self-assigned this Nov 5, 2002
@gvanrossum gvanrossum added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 5, 2002
@gvanrossum gvanrossum self-assigned this Nov 5, 2002
@gvanrossum gvanrossum added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 5, 2002
@gvanrossum
Copy link
Member Author

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"

???

@gvanrossum
Copy link
Member Author

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.

@birkenfeld
Copy link
Member

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

@gvanrossum
Copy link
Member Author

I don't think so. Closing.

@xiaq
Copy link
Mannequin

xiaq mannequin commented Jul 29, 2012

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.

@mstefanro
Copy link
Mannequin

mstefanro mannequin commented Jul 29, 2012

Only an issue in Python2.

    >>> A.B.__qualname__
    'A.B'
    >>> repr(A.B)
    "<class '__main__.A.B'>"

@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