473,780 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Introspection and member ordering

Rim
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:
class a: .... def __init__(self):
.... self.value = 12
.... self.avalue = 78 class b(a): .... def __init__(self):
.... a.__init__(self )
.... self.bvalue = 5
.... def f(self):
.... self.z = 3
.... c=b()
c.__dict__

{'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
Jul 18 '05 #1
1 1471
> 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:
class a: ... def __init__(self):
... self.value = 12
... self.avalue = 78 class b(a): ... def __init__(self):
... a.__init__(self )
... self.bvalue = 5
... def f(self):
... self.z = 3
... c=b()
c.__dict__

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


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:
http://aspn.activestate.com/ASPN/Coo.../Recipe/252524

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

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2656
by: Benjamin Rutt | last post by:
I'm trying to learn about introspection in Python. my ultimate goal is to be able to build a module "text database" of all modules that are in the sys.path, by discovering all candidate modules (I've already done that), importing all of them, and then introspecting on each module to discover its functions, globals and classes. But for now I am having a problem with the latter. I would like to import a module and figure out the names of...
0
1414
by: Steven T. Hatton | last post by:
I suspect the core language is not the level at which introspection should be implemented in C++. That has been the choice of C#, and Java. Both of these languages made some trade-offs to accomplish this. Like threading, introspection can probably be implemented by third party providers. I tend to favor open standards that are acceptted and respected by a significant portion of the developers working in the field. Stroustrup states the...
4
1838
by: Steven T. Hatton | last post by:
Has there been any substantial progress toward supporting introspection/reflection in C++? I don't intend to mean it should be part of the Standard. It would, nonetheless, be nice to have a generally accepted means of providing introspection. My inclination is to have two general categories of introspection: dynamic introspection, and static intospection. In situations where it makes sense to use virtual functions and their...
1
2331
by: James Geurts | last post by:
Hi, Can someone tell me how to test if a field is a const? I am using the FxCop introspection engine, but I suppose I could use reflection if required. for example, the following would return true: private const string DEFAULT_STRING = "Default"; thanks
2
2006
by: greg.corson | last post by:
Hi, I'm in the process of building a complex system using a sort of "system services" model. I'm looking at putting together various services like serialization, networking, 2D/3D rendering, debugger-like class inspection and other assorted stuff, but I need a way to bind them to other people's C++ code at runtime using external scripting. In particular I need a way to be able to access members of a class at runtime by name, and I...
8
3323
by: R. Bernstein | last post by:
In doing the extension to the python debugger which I have here: http://sourceforge.net/project/showfiles.php?group_id=61395&package_id=175827 I came across one little thing that it would be nice to get done better. I notice on stack traces and tracebacks, an exec or execfile command appears as a stack entry -- probably as it should since it creates a new environment. However the frame information for exec or execfile looks like this:...
8
1857
by: JDT | last post by:
Hi, The last statement at the end of this post is to delete a set member 7.7. I set a break point inside ltstr::operator(). I found out that STL binary-seach the set twice. How come? I have another problem that a member in a set cannot be deleted. I think I may be able to solve that problem if I can figure out the problem posted here. Thanks for your help. The sequence of (f1, f2) STL goes through is (7.7, 4.4), (7.7, 6.6),
3
1350
by: James Stroud | last post by:
Hello, I wanted to automagically generate an instance of a class from a dictionary--which might be generated from yaml or json. I came up with this: # automagical constructor def construct(cls, adict): dflts = cls.__init__.im_func.func_defaults vnames = cls.__init__.im_func.func_code.co_varnames
14
4211
by: Dave Rahardja | last post by:
Is there a way to generate a series of statements based on the data members of a structure at compile time? I have a function that reverses the endianness of any data structure: /// Reverse the endianness of a data structure "in place". template <typename T> void reverseEndian(T&); Using boost, it is possible to provide the default implementation for all POD
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10139
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6727
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.