473,769 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

introspection

How do I import a module and then ask it to show me its methods or other
aspects about itself during execution? I'd like to do something such as
this:

import win32api

print win32api.method s()

I'd like to write some test scripts that load modules and probe them for
information about themselves.
Sep 1 '06 #1
4 1398

brad tilley wrote:
How do I import a module and then ask it to show me its methods or other
aspects about itself during execution? I'd like to do something such as
this:

import win32api

print win32api.method s()

I'd like to write some test scripts that load modules and probe them for
information about themselves.
Hi Brad,

You might like to look at the built-in inspect module.

Cheers,
John

Sep 1 '06 #2
brad tilley <rt*****@vt.edu writes:
How do I import a module and then ask it to show me its methods or other
aspects about itself during execution? I'd like to do something such as
this:

import win32api

print win32api.method s()

I'd like to write some test scripts that load modules and probe them
for information about themselves.
As well as the 'inspect' module, you may be able to get some distance
just with builtin functionality::
>>import re
>>dir(re)
['DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE',
'S', 'U', 'UNICODE', 'VERBOSE', 'X', '__all__', '__builtins__',
'__doc__', '__file__', '__name__', 'compile', 'engine', 'error',
'escape', 'findall', 'finditer', 'match', 'purge', 'search',
'split', 'sub', 'subn', 'template']
>>[(n, getattr(re, n)) for n in dir(re)]
[('DOTALL', 16), ('I', 2), ('IGNORECASE', 2), ('L', 4), ('LOCALE',
4), ('M', 8), ('MULTILINE', 8), ('S', 16), ('U', 32), ('UNICODE',
32), ('VERBOSE', 64), ('X', 64), ('__all__', ['match', 'search',
'sub', 'subn', 'split', 'findall', 'compile', 'purge', 'template',
'escape', 'I', 'L', 'M', 'S', 'X', 'U', 'IGNORECASE', 'LOCALE',
'MULTILINE', 'DOTALL', 'VERBOSE', 'UNICODE', 'error',
'finditer']),
[...]
]
>>[n for n in dir(re) if hasattr(getattr (re, n), '__call__')]
['compile', 'escape', 'findall', 'finditer', 'match', 'purge',
'search', 'split', 'sub', 'subn', 'template']

--
\ "Madness is rare in individuals, but in groups, parties, |
`\ nations and ages it is the rule." -- Friedrich Nietzsche |
_o__) |
Ben Finney

Sep 1 '06 #3
brad tilley wrote:
How do I import a module and then ask it to show me its methods or other
aspects about itself during execution? I'd like to do something such as
this:

import win32api
dir(win32api)
help(win32api)

and also

import inspect
help(inspect)

</F>

Sep 1 '06 #4
Fredrik Lundh wrote:
brad tilley wrote:
>How do I import a module and then ask it to show me its methods or
other aspects about itself during execution? I'd like to do something
such as this:

import win32api

dir(win32api)
help(win32api)

and also

import inspect
help(inspect)

</F>
Great info guys! Thanks.
Sep 1 '06 #5

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

Similar topics

1
1471
by: Rim | last post by:
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):
2
1590
by: Vsevolod (Simon) Ilyushchenko | last post by:
Hi, Last year I have written a Perl module to serve as a backend to Macromedia Flash applications: http://www.simonf.com/amfperl I have just rewritten it in Python, which I have not used before. Thus, a couple of questions which I was not able to solve with online research: 1. To send an arbitrary object to Flash, I would like to use introspection to find out the values of all its instance variables. How
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
2330
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
2005
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:...
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
9583
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
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10039
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
9990
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
9860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5297
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
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3560
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.