2 Replies - 625 Views - Last Post: 10 January 2011 - 05:19 PM Rate Topic: -----

#1 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 119
  • View blog
  • Posts: 716
  • Joined: 26-May 10

Lists of Objects

Posted 10 January 2011 - 04:54 PM

I have a quick question about making lists of objects. First here's my understanding of how it's done:

object = Object()
objects.append(object)


This works just fine until I want to create a second object. Take this for example:

for i in range(0,3):
    object = Object()
    objects.append(object)


It seems to me that the objects would just be overwriting each other and only the last one would remain. As it is I can't dynamically create new objects. I'm sure this is just me missing some important detail about lists. Can someone tell me how this is done?

Edit: It just occurred to me that I could do it like this once:

for i in range(0,3):
    i = Object()
    objects.append(i)


...I think. Python for loops still confuse me. But either way I could only run that loop once and have it work correctly. It seems like I'm still in the same boat.

This post has been edited by Ntwiles: 10 January 2011 - 04:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Lists of Objects

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Lists of Objects

Posted 10 January 2011 - 05:07 PM

What is the question here?
It works exactly like your first code block:
>>> class Object():
	pass

>>> objects = []
>>> for i in range(0, 3):
	object = Object()
	objects.append(object)

	
>>> objects
[<__main__.Object instance at 0x0000000002B6EB48>, <__main__.Object instance at 0x0000000002B6EBC8>, <__main__.Object instance at 0x0000000002B6EC08>]



The only caveat here is that you shouldn't use the reserved word "object" for a variable name, as it may be needed later for some other use.

This is one of the wonderful features of Object References. The standard implementation of Python (CPython) uses reference counting for all objects. As soon as there is no way to reference an object, the object is a candidate for Garbage Collection. In the case above, even though the local variable (object reference) "object" is being overwritten, there is a second reference to that object, existing in the list "objects".

Here's a more in-depth look at Python's Data Model.
Was This Post Helpful? 1
  • +
  • -

#3 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 119
  • View blog
  • Posts: 716
  • Joined: 26-May 10

Re: Lists of Objects

Posted 10 January 2011 - 05:19 PM

View PostMotoma, on 10 January 2011 - 04:07 PM, said:

What is the question here?
It works exactly like your first code block:
>>> class Object():
	pass

>>> objects = []
>>> for i in range(0, 3):
	object = Object()
	objects.append(object)

	
>>> objects
[<__main__.Object instance at 0x0000000002B6EB48>, <__main__.Object instance at 0x0000000002B6EBC8>, <__main__.Object instance at 0x0000000002B6EC08>]



The only caveat here is that you shouldn't use the reserved word "object" for a variable name, as it may be needed later for some other use.

This is one of the wonderful features of Object References. The standard implementation of Python (CPython) uses reference counting for all objects. As soon as there is no way to reference an object, the object is a candidate for Garbage Collection. In the case above, even though the local variable (object reference) "object" is being overwritten, there is a second reference to that object, existing in the list "objects".

Here's a more in-depth look at Python's Data Model.


Huh. I thought what was happening was that each new object I made was overwriting the last. I guess my mistake was somewhere else. And to make me look like even more of an ass the objects seem to be creating just fine now (: . Thanks for clearing that up though. I need to get myself a better understanding of Python lists.

Btw the variable names were just for example. I'm actually trying to make instances of Bullet objects.

This post has been edited by Ntwiles: 10 January 2011 - 05:20 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1