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

Add in declaration order support for the dictionary passed in to the meta class __init__ and __new__ methods #53889

Closed
carstenkleinaxn-softwarede mannequin opened this issue Aug 25, 2010 · 3 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@carstenkleinaxn-softwarede
Copy link
Mannequin

BPO 9680
Nosy @merwok, @bitdancer

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 = None
closed_at = <Date 2010-08-25.12:43:03.917>
created_at = <Date 2010-08-25.09:21:40.720>
labels = ['interpreter-core', 'type-feature']
title = 'Add in declaration order support for the dictionary passed in to the meta class __init__ and __new__ methods'
updated_at = <Date 2010-08-25.13:38:24.274>
user = 'https://bugs.python.org/carstenkleinaxn-softwarede'

bugs.python.org fields:

activity = <Date 2010-08-25.13:38:24.274>
actor = 'eric.araujo'
assignee = 'none'
closed = True
closed_date = <Date 2010-08-25.12:43:03.917>
closer = 'r.david.murray'
components = ['Interpreter Core']
creation = <Date 2010-08-25.09:21:40.720>
creator = 'carsten.klein@axn-software.de'
dependencies = []
files = []
hgrepos = []
issue_num = 9680
keywords = []
message_count = 3.0
messages = ['114890', '114898', '114905']
nosy_count = 3.0
nosy_names = ['eric.araujo', 'r.david.murray', 'carsten.klein@axn-software.de']
pr_nums = []
priority = 'normal'
resolution = 'out of date'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue9680'
versions = ['Python 3.1', 'Python 3.2']

@carstenkleinaxn-softwarede
Copy link
Mannequin Author

Example

class Meta(type):
    def __new__(cls, name, bases, locals):
        print repr(locals.keys())

class Test(object):
    __metaclass__ = Meta
    A = 1
    B = 2
    C = 3
    D = 4
    E = 5

The above will yield the keys in a somewhat random order, everytime you start up the Python interpreter:

['__module__', 'E', 'C', 'D', 'B', '__metaclass__', 'A']

While the above example is far from complete, it shows the basic dilemma when having some concept that relies on the order in which the elements have been declared and in the order by which they have been processed during the parse phase and ast traversal phase.

In the aforementioned first two phases one can rely on the declaration order, but as soon as we enter the __new__ method, the order becomes irrelevant and is completely lost.

For a framework of mine, I would like the locals dict that is being passed as an argument to the __new__ method to give out references to the keys in the order in which they have been declared in the dict.

Thus, the above example would yield

['__metaclass__', '__module__', 'A', 'B', 'C', 'D', 'E']

The basic reason is that I find it more intuitive to

class A(object):
  __metaclass__ = Meta
  A = 5
  Z = 9

than

class A(object):
  __metaclass__ = Meta
  __fields__ = ((A,5), (Z,9))

As you might easily guesses, the main application for the above is a new enum type I am currently developing, where the order is important as every new instance of that class must always yield the same ordinals for the individual constants declared.

This should not break with the overall contract of the dict, which defines that keys returned are in no specific order. Thus, adding a specific order to keys in the above locals dict for class instantiation purposes only, would not break with existing code and should be both backwards and forwards compatible.

@carstenkleinaxn-softwarede carstenkleinaxn-softwarede mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Aug 25, 2010
@bitdancer
Copy link
Member

The ordering of dictionary keys is a fundamental property of Python dictionaries (it's a hash table). PEP-3115 provides the functionality you are looking for, your metaclass just needs to be slightly more complicated.

@merwok
Copy link
Member

merwok commented Aug 25, 2010

For reference, a shorter explanation that the PEP is http://docs.python.org/dev/reference/datamodel#customizing-class-creation

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 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) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants