Introspection, importing and Flash

Discussion in 'Python' started by Vsevolod (Simon) Ilyushchenko, Jun 29, 2004.

  1. 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
    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.

    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.

    In Python, the most concise way I found was to have AMFPython.py in the
    AMF directory, place __init__.py with "import AMFPython" in the AMF
    directory, then say in my top-level script
    import AMF
    instance = AMF.AMFPython.AMFPython()
    Which is rather redundant. I'd like to do something not more complicated
    than:

    import AMF.AMFPython as AMF
    instance = AMF()

    but I don't know if it's possible.

    Thanks,
    Simon
    --

    Simon (Vsevolod ILyushchenko)
    http://www.simonf.com

    Terrorism is a tactic and so to declare war on terrorism
    is equivalent to Roosevelt's declaring war on blitzkrieg.

    Zbigniew Brzezinski, U.S. national security advisor, 1977-81
     
    Vsevolod (Simon) Ilyushchenko, Jun 29, 2004
    #1
    1. Advertising

  2. > 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.
     
    Christopher T King, Jun 29, 2004
    #2
    1. Advertising

  3. Vsevolod (Simon) Ilyushchenko

    Follower Guest

    > What you seek is the dir() function, in concert with the getattr()
    > function.

    Note the standard `dir()` proviso:

    "Because dir() is supplied primarily as a convenience for use at an
    interactive prompt, it tries to supply an interesting set of names
    more than it tries to supply a rigorously or consistently defined set
    of names, and its detailed behavior may change across releases."

    -- <http://www.python.org/doc/current/lib/built-in-funcs.html>

    Off the top of my head I'd say you'd use `__slots__` if it exists,
    otherwise use `__dict__`. But I'm thinking this is possibly a FAQ that
    has a "proper" answer someone else will point to.

    --Phil.
     
    Follower, Jul 1, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bernie
    Replies:
    0
    Views:
    556
    Bernie
    Sep 5, 2004
  2. Jim Higson
    Replies:
    1
    Views:
    519
    Jim Higson
    Sep 21, 2004
  3. Replies:
    8
    Views:
    23,520
    SABOND21
    Feb 9, 2012
  4. Rim
    Replies:
    1
    Views:
    242
    Josiah Carlson
    Jan 21, 2004
  5. plb
    Replies:
    2
    Views:
    356
Loading...

Share This Page