472,791 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Introspection, importing and Flash

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) si****@cshl.edu
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

Jul 18 '05 #1
2 1542
> 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.

Jul 18 '05 #2
> 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.
Jul 18 '05 #3

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

Similar topics

8
by: Mark English | last post by:
I'd like to write a Tkinter app which, given a class, pops up a window(s) with fields for each "attribute" of that class. The user could enter values for the attributes and on closing the window...
4
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...
0
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...
4
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...
14
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...
0
by: AceKombat | last post by:
Hi ! I'm having a problem after importing a gif into flash . When the gif is imported , it shows in the library tab and looks as it is successfully imported , but no , for some reason the last frame...
1
by: Zack9236 | last post by:
Hello All, I am pulling my hair out over this deal. First off, I'm a Flash newbie and I'm using the Creating a Web Site with Flash CS3 Professional book to help me create my website. Most...
1
by: dolittle | last post by:
Hi, I'm new to flex and trying to understand a tutorial by reading the code. There is a line: var mics:Array = Microphone.names; Flex docs says that you need to import flash.media.Microphone...
5
by: phpmagesh | last post by:
Hi All, I have html page, in that i have some flash header. In that flash header i have sound on/off function. i implemented this in my html page. but i need some modification in this. now it...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.