Module listing in order.

Diez B. Roggisch deets at nospam.web.de
Wed May 23 06:09:16 EDT 2007


Ramashish Baranwal wrote:

> Hi,
> 
> I want to get a module's contents (classes, functions and variables)
> in the order in which they are declared. Using dir(module) therefore
> doesn't work for me as it returns a list in alphabetical order. As an
> example-
> 
> # mymodule.py
> class B: pass
> class A: pass
> class D: pass
> 
> # test.py
> import mymodule
> # This returns['A', 'B', 'D', '__builtins__', '__doc__', '__file__',
> '__name__']
> contents = dir(mymodule)
> 
> I want a way to get the contents in the order of their declaration,
> i.e. [B, A, D]. Does anyone know a way to get it?

Whatfor do you actually need this? Is it a general interest - then things
get difficult. But for certain usecases, metaclasses might come to the
rescue. But that depends on what you want to do.

Diez



More information about the Python-list mailing list