wbruntra
wbruntra

Reputation: 1061

How can I name my objects in Python?

I know this is a very simple question, but unfortunately I don't know enough to search effectively for an answer. Any answers or links to things I should already know would be greatly appreciated.

What I am trying to do is make an environment in Python where I have a bunch of turtles running around doing various things (it's basically like StarLogo from MIT).

class Turtle:
    def __init__(self,x,y):
        self.xpos = float(x)
        self.ypos = float(y)
        self.head = 0.0

numTurtles = 4
for i in range(numTurtles):
    ...
MyTurtles = [...]

Each turtle has an x position, a y position, and a heading (in radians). All I want to do is create them and tell them where to stand (their x and y coordinates), and then put all the turtles into a list so that later I can tell the whole group to take a certain action.

I want to be able to change the number of turtles, otherwise I would just call them [a,b,c,d]. But I figure there has to be a better way.

Upvotes: 1

Views: 180

Answers (4)

Skurmedel
Skurmedel

Reputation: 22149

If you want them to be accessible by some unique name, but avoid having a billion variables, you could always store them in a dictionary.

{ "Norma": Turtle(1, 2), "Kyle": Turtle(3, 4) }

You can modify a dictionary after it's been created, appending and deleting as you like.

turtles["Norma"] = Turtle(30, 40) # we just replaced Norma
turtles["Rackham"] = Turtle(0, 1) # a new turtle added.

There are a number of ways to generate such a dictionary, should you not want to make it by hand.

Using zip we can take two iterables, and make pairs out of the consecutive values:

zip(["Norma", "Rackham"], [Turtle(1, 2), Turtle(0, 1)])

The result is an interable returning tuples (dimension is decided by amount of arguments given to zip.)

Handily enough, the dictionary constructor can take such a list:

dict(zip(["Norma", "Rackham"], [Turtle(1, 2), Turtle(0, 1)]))

Et voilá. A dictionary.

You can also use a dictionary expression (availability depends on Python version):

{ name: turtle for name, turtle in zip(names, turtles) }

Upvotes: 2

Bryce Siedschlaw
Bryce Siedschlaw

Reputation: 4226

class Turtle:
    def __init__(self,x,y):
        self.xpos = float(x)
        self.ypos = float(y)
        self.head = 0.0
    def certain_action():
        # Do action

numTurtles = 4
MyTurtles = []
# Need to set x and y
for i in range(numTurtles):
    MyTurtles.append(Turtle(x,y))

for t in MyTurtles:
    t.certain_action()

Upvotes: 0

Jakob Bowyer
Jakob Bowyer

Reputation: 34698

class Turtle(object):
    def __init__(self,x,y,name):
        self.xpos = float(x)
        self.ypos = float(y)
        self.head = 0.0
        self.name = name

my_turtles = []
for name, i in zip(xrange(num_turtles), names):
    x = ...
    y = ...
    h = ...
    my_turtles.append(Turtle(x, y, h, name))

Upvotes: 0

Sven Marnach
Sven Marnach

Reputation: 601559

You should add the turtles to the list directly while executing the loop, for example

my_turtles = []
for i in range(num_turtles):
    x = ...
    y = ...
    h = ...
    my_turtles.append(Turtle(x, y, h))

It's often also possible to write this as a "list comprehension":

my_turtles = [Turtle(..., ..., ...) for i in range(num_turtles)]

Upvotes: 6

Related Questions