8000 Listing class attributes in the defined order · Issue #5322 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

Listing class attributes in the defined order #5322

@andrewleech

Description

@andrewleech

I've got a config file management package I'm trying to port to micropython, where the config file structure is defined as a python class: https://pypi.org/project/structured-config/

Some of the features of this module are dependent on knowing the order and iterating over of attributes defined in a class.

This kind of ordered attributes access can also be useful for various dataclass type behaviors.

Currently the only way I can find to get attributes listed on a class is with dir()

>>> bar = None
>>> 
>>> class Structure:
>>> 	first = 1
>>> 	foo = 0x0f
>>> 	bar = 0x0b
>>> 	last = 2
>>> 
>>> print(dir(Structure))
['__class__', '__module__', '__name__', '__qualname__', '__bases__', 'bar', 'first', 'foo', 'last']

However, looking at the source for dir(), I can see that that it works by iterating over every QSTR defined in micropython and checks the class/object for each one to see if it exists (seems a bit backwards to me) so they get listed in the order they're (ever) defined in micropython
https://github.com/micropython/micropython/blob/master/py/modbuiltins.c#L175

Is there any other way in micropython currently this could be accomplished?

Metadata

Metadata

Assignees

No one assigned

    Labels

    py-coreRelates to py/ directory in source

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0