470,855 Members | 1,111 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

Dynamic Class Creation

(maybe it works better with Subject ;))

Hi there,
my problem: i have a directory, where several python modules (classes) are
stored.
The names of these modules arent known before runtime. Now i want to parse
the Directory and create an instance of each class. I tried to handle this
with
new.instance() and new.clasobj() but this didnt really work for me. Any
Ideas ?
Jul 18 '05 #1
2 1440

"Steffen Schoen" <py*********@gmx.de> wrote in message
news:ma**************************************@pyth on.org...
(maybe it works better with Subject ;))

Hi there,
my problem: i have a directory, where several python modules (classes) are
stored.
The names of these modules arent known before runtime. Now i want to parse
the Directory and create an instance of each class. I tried to handle this
with
new.instance() and new.clasobj() but this didnt really work for me. Any
Ideas ?


You could try something like,
import os
import imp
import glob
modualnames = glob.glob('mydir\\*\\*.py')
classdict = {}
for i in modualnames:
flnm = file(i)
nm = os.path.splitext(os.path.basename(i))[0]
classdict[nm] = imp.load_module(nm ,flnm ,i,'')


check out the imp module in the Global Module Index.

Tom
Jul 18 '05 #2

I have written a module which
creates classes dynamically,
but some Python tools, e.g.
PyChecker crash if they try
to analyse the source.
So be careful!

Ciao,
Dominic
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Eddie de Bear | last post: by
4 posts views Thread by Leslaw Bieniasz | last post: by
1 post views Thread by andrew queisser | last post: by
15 posts views Thread by rwf_20 | last post: by
reply views Thread by Pascal Costanza | last post: by
1 post views Thread by None | last post: by
16 posts views Thread by manatlan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.