Create object for item in list

Discussion in 'Python' started by Helge, Apr 29, 2004.

  1. Helge

    Helge Guest

    I wonder how this can be accomplished:

    I've got a list, containing several strings. The strings look like
    this:

    [ "Name1/N1/N2/N3" , "Name2/N1/N2/N3" , "..." ]

    I would like to create one object for each item in the list, and the
    name of the object should be "str(item.split("/")[0])".

    I've tried this, but it won't work:

    for item in list:
    str(item.split("/")[0]) = class(item)

    ....
    Helge, Apr 29, 2004
    #1
    1. Advertising

  2. Helge

    Russell Blau Guest

    "Helge" <> wrote in message
    news:...
    > I wonder how this can be accomplished:
    >
    > I've got a list, containing several strings. The strings look like
    > this:
    >
    > [ "Name1/N1/N2/N3" , "Name2/N1/N2/N3" , "..." ]
    >
    > I would like to create one object for each item in the list, and the
    > name of the object should be "str(item.split("/")[0])".
    >
    > I've tried this, but it won't work:
    >
    > for item in list:
    > str(item.split("/")[0]) = class(item)
    >
    > ...


    A couple of small points before getting to the big issue:

    1. "class" is a keyword, and can't be used in the context you have it. You
    should define your class (which I'll call "C") earlier in your program.

    2. "list" is a type name, and shouldn't be used as a variable name, so I'll
    change it to "mylist".

    2. The str() function is unnecessary, since item.split("/")[0] is already a
    string by definition.

    Now, I assume what you are trying to do is get Python to create an instance
    of your class C, and assign it to the variable whose name is given by the
    evaluation of item.split("/")[0]. In other words, you want Python to
    execute a series of statements that look like this:

    Name1 = C("Name1/N1/N2/N3")

    What you've done doesn't quite accomplish this; the left side of your
    assignment expression evaluates to a string, so in effect you're telling
    Python to execute:

    "Name1" = C("Name1/N1/N2/N3")

    (At least that's how I interpret what you mean by "class(item)".) This
    doesn't make sense to the interpreter, any more than "Spam" = 7 would. But
    you can get Python to execute code provided to it inside a string, as
    follows:

    for item in mylist:
    exec "%s = C('%s')" % (item.split("/")[0], item)

    Good luck... Needless to say, you need to be careful about where the
    contents of "mylist" come from, or you could end up overwriting other
    variables that your program needs to function correctly.

    --
    I don't actually read my hotmail account, but you can replace hotmail with
    excite if you really want to reach me.
    Russell Blau, Apr 29, 2004
    #2
    1. Advertising

  3. Helge

    Terry Reedy Guest

    "Helge" <> wrote in message
    news:...
    > I wonder how this can be accomplished:
    >
    > I've got a list, containing several strings. The strings look like
    > this:
    >
    > [ "Name1/N1/N2/N3" , "Name2/N1/N2/N3" , "..." ]
    >
    > I would like to create one object for each item in the list, and the
    > name of the object should be "str(item.split("/")[0])".


    Binding and using dynamic names in the global (or function local) namespace
    is generally a nuisance. The standard idiom is to use a separate
    dictionary as a namespace for just those items. Something like

    myobs = {}
    for datastring in namedata:
    data = datastring.split('/')
    myobs[data[0]] = myclass(*data[1:])

    Terry J. Reedy
    Terry Reedy, Apr 29, 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. ssoss

    Display Related Item or Add Item

    ssoss, Sep 17, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    482
    ssoss
    Sep 19, 2003
  2. Replies:
    2
    Views:
    608
  3. Helge

    Create object for item in list

    Helge, Apr 29, 2004, in forum: Python
    Replies:
    0
    Views:
    282
    Helge
    Apr 29, 2004
  4. jon wayne
    Replies:
    9
    Views:
    697
    Jim Langston
    Sep 22, 2005
  5. QUASAR
    Replies:
    6
    Views:
    409
    QUASAR
    Jan 17, 2004
Loading...

Share This Page