View Single Post
  #4 (permalink)  
Old 03-27-2008, 01:05 AM
Sharke Sharke is offline
Newbie
 
Join Date: Nov 2007
Posts: 6
Rep Power: 0
Sharke is on a distinguished road
Default Re: Naming objects in python

I may have misunderstood here, but wasn't what he wanted the ability to name objects after programmatically generated strings?

So instead of saying a = test(i), he wanted to generate a string, and then name the object after that string? So if name = "Bob", he needs to know how to create an object using name as the variable name, so that Bob = test(i).

I don't know how to do that. And I guess I can't work out why you'd want to do that anyway, if that what you DID mean! If you didn't, then I guess the first reply pretty much covers it!

Actually if something like that is what you wanted to do, I suppose you could implement a dictionary with your strings as keys and objects as values, as in..
Code:
class test:
        def __init__(self,x):
        self.x = x

d = {}
for i in range (1,11):
    name = chr(96 + i)

d[name] = test(i)

Last edited by Sharke; 03-27-2008 at 01:08 AM.
Reply With Quote