|
||||||
| Delphi/Python Forum for discussing Borland Delphi and Python coding techniques, tips and tricks. Ask your python questions here. |
![]() |
|
|
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 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.
|
| 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 | Delphi/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 | Delphi/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 | Delphi/Python | 0 | 07-05-2006 10:27 PM |