Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Delphi/Python

Delphi/Python Forum for discussing Borland Delphi and Python coding techniques, tips and tricks. Ask your python questions here.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-23-2008, 09:12 PM
Deep fried ice-cream Deep fried ice-cream is offline
Newbie
 
Join Date: Feb 2008
Location: Wellington, NZ
Posts: 3
Rep Power: 0
Deep fried ice-cream is on a distinguished road
Send a message via Skype™ to Deep fried ice-cream
Question Naming objects in python

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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 03-24-2008, 03:08 AM
v0id's Avatar   
v0id v0id is offline
Super Mod
 
Join Date: Apr 2007
Location: Denmark
Posts: 1,940
Last Blog:
CherryPy(thon)
Rep Power: 23
v0id is a splendid one to beholdv0id is a splendid one to beholdv0id is a splendid one to beholdv0id is a splendid one to beholdv0id is a splendid one to beholdv0id is a splendid one to behold
Send a message via MSN to v0id
Default Re: Naming objects in python

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()
I've defined a function, get, so you can see that there really is multiple objects, with different values. If you want a nicer representation, than using get, you can overload either __repr__ or __str__.
Code:
# ...

    def __str__(self):
        return str(self.value)

# ...

for index in range(0, len(listOfObjects)):
    print listOfObjects[index]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-25-2008, 04:20 AM
Deep fried ice-cream Deep fried ice-cream is offline
Newbie
 
Join Date: Feb 2008
Location: Wellington, NZ
Posts: 3
Rep Power: 0
Deep fried ice-cream is on a distinguished road
Send a message via Skype™ to Deep fried ice-cream
Default Re: Naming objects in python

Thanks so much for that!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-27-2008, 01:49 AM
v0id's Avatar   
v0id v0id is offline
Super Mod
 
Join Date: Apr 2007
Location: Denmark
Posts: 1,940
Last Blog:
CherryPy(thon)
Rep Power: 23
v0id is a splendid one to beholdv0id is a splendid one to beholdv0id is a splendid one to beholdv0id is a splendid one to beholdv0id is a splendid one to beholdv0id is a splendid one to behold
Send a message via MSN to v0id
Default Re: Naming objects in python

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT -5. The time now is 01:53 AM.

Contest Stats

dargueta ........ 128.00000
John ........ 127.00000
Xav ........ 107.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

Ads