Introspection and member ordering

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rim

    Introspection and member ordering

    Hi,

    Is there a way to get any sort of introspection which would return the
    content of an instance *in the order they were declared or executed*
    as opposed to alphabetical order?

    Example:
    [color=blue][color=green][color=darkred]
    >>> class a:[/color][/color][/color]
    .... def __init__(self):
    .... self.value = 12
    .... self.avalue = 78[color=blue][color=green][color=darkred]
    >>> class b(a):[/color][/color][/color]
    .... def __init__(self):
    .... a.__init__(self )
    .... self.bvalue = 5
    .... def f(self):
    .... self.z = 3
    ....[color=blue][color=green][color=darkred]
    >>> c=b()
    >>> c.__dict__[/color][/color][/color]
    {'bvalue': 5, 'avalue': 78, 'value': 12}

    Wrong answer, I am looking for something that would give me
    {'value':12, 'avalue':78, 'bvalue':5'}

    I tried the inspect.getmemb ers(c) without success.

    Anyone has some advice how I can get the members listed in declaration
    or execution order?

    Thanks
    -Rim
  • Josiah Carlson

    #2
    Re: Introspection and member ordering

    > Is there a way to get any sort of introspection which would return the[color=blue]
    > content of an instance *in the order they were declared or executed*
    > as opposed to alphabetical order?
    >
    > Example:
    >[color=green][color=darkred]
    > >>> class a:[/color][/color]
    > ... def __init__(self):
    > ... self.value = 12
    > ... self.avalue = 78[color=green][color=darkred]
    > >>> class b(a):[/color][/color]
    > ... def __init__(self):
    > ... a.__init__(self )
    > ... self.bvalue = 5
    > ... def f(self):
    > ... self.z = 3
    > ...[color=green][color=darkred]
    > >>> c=b()
    > >>> c.__dict__[/color][/color]
    > {'bvalue': 5, 'avalue': 78, 'value': 12}
    >
    > Wrong answer, I am looking for something that would give me
    > {'value':12, 'avalue':78, 'bvalue':5'}
    >
    > I tried the inspect.getmemb ers(c) without success.
    >
    > Anyone has some advice how I can get the members listed in declaration
    > or execution order?[/color]

    Rim,

    You'd have to either overload the class's __getattribute_ _, __setattr__
    and __delattr__ methods, or replace the builtin instance.__dict __
    attribute with a new dictionary-like object that will produce the
    ordering you want.

    As an option, I've written an LRU that does such kinds of updates
    quickly, though it also updates on read. Reading the comment at the
    bottom will allow you to translate the update on read, to only updating
    on write. The recipe is available here:


    Doing a quick check, it doesn't look like you can say:
    self.__dict__ = NON_DICT_OBJECT

    Making LRU a subclass of dict seems to work on the surface, but it looks
    to break a few things. Perhaps I should rewrite LRU to be more Pythonic.

    Until then, you'll have to update __getattribute_ _, __setattr__ and
    __delattr__.

    - Josiah

    Comment

    Working...