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: __name__ available during class dictionary build
Type: enhancement Stage:
Components: None Versions:
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, moese
Priority: normal Keywords:

Created on 2005-10-11 22:15 by moese, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg61212 - (view) Author: Moese (moese) Date: 2005-10-11 22:15
It would be nice if __name__ would be defined in the
class block during it's construction. I have a code
generator which generates C-structure wrappers and this
would make it cleaner.

> class Test(object):
>    print __name__
>    #print locals()["__name__"] - this should also work

It should print "Test" and not the name of the current
module (global __main__).

I looked through the source code to see how this could
be implemented, but I don't understand exactly what's
going on and I'm afraid I'll broke something since the
class name is stored in a slot.

Here's some sample code I'm using. I could get rid of
the extra "MSG" parameter if this is implemented.

> from Structure import Structure
> from px.Structure.POINT import POINT
>
> # WinUser.h
> class MSG(object):
>
>     _members = (
>         ("HWND", "hwnd"),
>         ("UINT", "message"),
>         ("WPARAM", "wParam"),
>         ("LPARAM", "lParam"),
>         ("DWORD", "time"),
>         (POINT, "pt"))
>
>     Structure(globals(), locals(), "MSG")
msg83317 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-03-08 17:38
This can be done with a metaclass:
the metaclass receives the name of the created class, and it's easy to 
update the class definition there.
History
Date User Action Args
2022-04-11 14:56:13adminsetgithub: 42470
2009-03-08 17:38:31amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg83317

resolution: works for me
2005-10-11 22:15:09moesecreate