Introspection, importing and Flash

Christopher T King squirrel at WPI.EDU
Tue Jun 29 12:17:00 EDT 2004


> 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 
> can I get their names? I've read about __dict__,  __slots__, and 
> __getattr__, but I am not sure what would be the most universal way to 
> get all the instance variable names from both old-style and new-style 
> objects. I would need something like __listattr__, which does not exist.

What you seek is the dir() function, in concert with the getattr()  
function. Note that dir() returns both hidden and visible methods as well
as instance variables. You can weed out the functions (if so desired) by
using the callable() function. Example:

for attrname in dir(object):
    attr=getattr(object,attrname)
    if not callable(attr):
        <do something>

> 2. Python class importing works in a rather confusing way. In Perl, I 
> have a top-level class AMF::Perl, which I "import" once and then create 
> its instance like this: "new AMF::Perl". The file Perl.pm lives in the 
> AMF directory.

Python modules can be either directories with __init__.py entries (as you 
have discovered) or a single file. If you rename AMFPython.py to AMF.py 
and place it in your program's main directory, you can then do either 
this:

import AMF
instance = AMF.AMFPython()

or this:

from AMF import AMFPython
instance = AMFPython()

The dotted import notation in Python is used to refer to modules contained 
in directories, not classes contained within modules.




More information about the Python-list mailing list