473,287 Members | 2,682 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

getting dir(x), but not as list of strings?

mh
I want to iterate over members of a module, something like:

for i in dir(x):
if type(i) == types.FunctionType: ...

but of course dir() returns a list of strings. If x is a module,
how can I get the list of its members as their actual types?

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios
Jun 27 '08 #1
4 1076
On Wed, 21 May 2008 07:54:32 +0000, mh wrote:
I want to iterate over members of a module, something like:

for i in dir(x):
if type(i) == types.FunctionType: ...

but of course dir() returns a list of strings. If x is a module,
how can I get the list of its members as their actual types?
Take a look at the `inspect` module.

Ciao,
Marc 'BlackJack' Rintsch
Jun 27 '08 #2
Use

for i in dir(x):
print i # name of member as string
print getattr(x, i) # member

regards

Arno
On 21 Mai, 09:54, m...@pixar.com wrote:
I want to iterate over members of a module, something like:

for i in dir(x):
if type(i) == types.FunctionType: ...

but of course dir() returns a list of strings. If x is a module,
how can I get the list of its members as their actual types?

Many TIA!
Mark
Jun 27 '08 #3
mh@pixar.com wrote:
I want to iterate over members of a module, something like:

for i in dir(x):
if type(i) == types.FunctionType: ...

but of course dir() returns a list of strings. If x is a module,
how can I get the list of its members as their actual types?

Many TIA!
Mark

Use the builtin vars to get a dictionary of names and associated objects.

import sys
for name,ob in vars(sys).items():
print name,type(ob)

Gary Herron

setrecursionlimit <type 'builtin_function_or_method'>
getfilesystemencoding <type 'builtin_function_or_method'>
path_importer_cache <type 'dict'>
stdout <type 'file'>
version_info <type 'tuple'>
exc_clear <type 'builtin_function_or_method'>
prefix <type 'str'>
getrefcount <type 'builtin_function_or_method'>
byteorder <type 'str'>
....

Jun 27 '08 #4

"Gary Herron" <gh*****@islandtraining.comwrote in message
news:48**************@islandtraining.com...
| mh@pixar.com wrote:
| I want to iterate over members of a module, something like:
| >
| for i in dir(x):
| if type(i) == types.FunctionType: ...
| >
| but of course dir() returns a list of strings. If x is a module,
| how can I get the list of its members as their actual types?
| >
| Many TIA!
| Mark
| >
| >
| Use the builtin vars to get a dictionary of names and associated objects.
|
| import sys
| for name,ob in vars(sys).items():
| print name,type(ob)

This also works on classes, but apparently not on most other objects.

Jun 27 '08 #5

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

Similar topics

0
by: Tetedeiench | last post by:
Hi ! I am currently changing the server for my website, and i make a heavy use of openssl functions. Both servers use PHP4.3.3 with OpenSSL 0.9.6i. I was actually testing with this code,...
6
by: Michael Hamm | last post by:
Hi, I'm trying to write a <form> whihc will retrieve a Web page on another server. I have (essentially) this: <form action="http://cgi.cs.indiana.edu/~oracle/digest.cgi"> <input...
8
by: Opa | last post by:
Hi, Does anyone know how to get a list of files for a given directory from a given url. I have the following, but get an error indicating that URI formats are not supported. ...
9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
3
by: dibblm | last post by:
Below is current code used. I can only list one directory then move to next. I want to search one more directory further and can't seem to find how to get one deeper. What I want to accomplish is...
5
by: Amjad | last post by:
Hi, I want to write a For loop that will put file names of extensions (*.txt and *.csv) in an array. I had it work fine for one extension, and I need help making it work for two extensions. My...
1
by: Scott | last post by:
Apparently VB.Net has a new behaviour for the DIR function and I quote: - ================================================================================================== In Visual Basic 6.0,...
14
by: jonathan184 | last post by:
Hi The script seems to be working ok with a list files to read a dir from but i got a dir with thousands of files and it does not search through all files, when i do a print of the read dir it comes...
2
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 with Apache 2 on Fedora Core 6 Linux. Given a directory, how would I get all sub-directories? I found "scandir" but that only applies for PHP 5 and I think it returns...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.