pass objects to exec

zunbeltz zunlatex at hotmail.com
Mon Mar 24 02:59:01 EST 2003


Jp Calderone <exarkun at intarweb.us> wrote in message news:<mailman.1048271015.20213.python-list at python.org>...
> On Fri, Mar 21, 2003 at 05:15:40AM -0800, zunbeltz wrote:
> > Hi
> > I'm new programming in Python (and other languages :) )
> > I've created a 
> > class Group(Menmbers)
> > 
> > Members is a list tahat I use to initialize a group
> > i.e. GroupA = Group(Members)
> > 
> > The class Group have a method superclass that return a list MemberList
> > I want to create new instances of Class Group each initialized with
> > each element of MembersList and I want that each new instance have a
> > name I can use i.e. GroupA-A, GroupA-B, ... GroupA-n where n depends
> > on len(MenmberList).
> > 
> 
>   Unfortunately, "-" is not allowed in identifiers in Python, so what you
> want is impossible.
> 
>   What you can do, however, is create a dict.
> 
>       d = {}
>       for i in range(len(MemberList)):
>           d['GroupA-%c' % (i + ord('A'))] = Group(MemberList[i])
> 
>   And you never even need to use exec ;)
> 
>   Jp
> 
> -- 
> Examinations are formidable even to the best prepared, for
> even the greatest fool may ask more the the wisest man can answer.
>                 -- C.C. Colton
Tanks for answer.  Group class need a second argument, it is a string
similar to that in the dictionary key. Can I use something like that
       d = {}
       for i in range(len(MemberList)):
           d['GroupA-%c' % (i + ord('A'))] =
Group(MemberList[i],'GroupA-%c' %(i+ òrd('A')))




More information about the Python-list mailing list