|
||||||
| Python Discussion forum for Python, a high-level language with simple syntax, but yet powerful. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
How do I make my python script create objects automatically? (e.g. After the contents of a variable).
I don’t actually have a source code as my project needs one of these to get started but I put in a simple example in case I’m not being very clear. Any help would be appreciated. Code:
class test:
def __init__(self,x):
self.x = x
for i in range (1,11):
name = chr(96 + i)
name = test(i)
|
| Sponsored Links |
|
|
|
|||||
|
I'm not very clear on what you want. Do you want to automatically create objects, using a loop? If so, then the code your provided can't be used. You're creating a new object for the same variable, name, all the time. To easily create multiple objects with a loop, you'll need to save the old objects in some way. This could be done using a list.
Code:
class Object:
def __init__(self, value):
self.value = value
def get(self):
return self.value
listOfObjects = []
for value in range(0, 10):
listOfObjects += [Object(value)]
for index in range(0, len(listOfObjects)):
print listOfObjects[index].get()
Code:
# ...
def __str__(self):
return str(self.value)
# ...
for index in range(0, len(listOfObjects)):
print listOfObjects[index]
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Finally we got that Python-forum! |
|
|||
|
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. |
|
|||||
|
I don't think he wanted to have objects called "after the contents of a variable", but I think he just wanted to use the variable for passing it to the constructor. But I could be wrong too. Anyways, it seems like (after looking at his reply) what I made for him was fine.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Finally we got that Python-forum! |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Python for developing software | Kasper | Python | 6 | 03-17-2008 11:45 AM |
| create a deck of cards with objects | damien | Java Help | 6 | 01-11-2008 02:24 PM |
| Python Collection | reachpradeep | Python | 1 | 03-03-2007 02:50 PM |
| Objects | Nightracer | General Programming | 8 | 07-28-2006 08:02 PM |
| Python was the first language I tried | Kaabi | Python | 0 | 07-05-2006 10:27 PM |