[Chicago] create all classes

Kumar McMillan kumar.mcmillan at gmail.com
Fri Feb 1 17:32:28 CET 2008


you want

import blocks, types
for name in dir(blocks):
    TheClass = getattr(blocks, name)
    if not type(TheClass)==types.ClassType:
        continue
    c = TheClass()

you might also need weed out any classes that are not the ones you are
trying to instantiate

On Feb 1, 2008 10:26 AM, Lukasz Szybalski <szybalski at gmail.com> wrote:
> Hello,
> I just converted big txt file definition to classes in .py file.
>
> There are around 30 classes in the file. How can I iterate through
> that file and initiate each class to some object in a list?
>
> ---blocks.py
>
> class MSG_CTRL_BLK(object):
>     def __init__(self):
>         self.block='025A'
>         self.GMSLOC=Element(self.block,'GLOC','MESSAGE HEADER','AN',26, 1)
>
> so instead of typing :
> a=MSG_CTRL_BLK()
> ....
> ...
> 30 times
> ....
>
> I want:
> Outside of the file I would like to :
> import blocks
> myblocks=[]
> for i in blocks.xx???Xx:
>     myblocks.append( i() )
>
> what would be the function to get me list of classes?
> How can I iterate and create them on a fly.
>
> Lucas
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>


More information about the Chicago mailing list