|
||||||
| 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 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 02: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.
|
| 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 | 8 | 08-07-2008 12:11 AM |
| create a deck of cards with objects | damien | Java Help | 6 | 01-11-2008 03:24 PM |
| Python Collection | reachpradeep | Python | 1 | 03-03-2007 03:50 PM |
| Objects | Nightracer | General Programming | 8 | 07-28-2006 09:02 PM |
| Python was the first language I tried | Kaabi | Python | 0 | 07-05-2006 11:27 PM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |