View Single Post
  #2 (permalink)  
Old 03-24-2008, 03:08 AM
v0id's Avatar   
v0id v0id is offline
Super Moderator
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,572
Last Blog:
CherryPy(thon)
Credits: 54
Rep Power: 28
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
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]
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
-
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
-
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

I'm always up for a chat, so feel free to contact me...
Reply With Quote