I have a class called Station
...
class Station():
def __init__(self):
self.connecting_stations = []
...a list with 3 Station
instances...
stations = [Station() for n in range(3)]
...and a for-loop that appends the string "New Hampshire"
to the connecting_stations
attribute of my three Station
instances.
for station in stations:
station.connecting_stations.append("New Hampshire")
When I print([station.connecting_stations for station in stations])
it prints:
[
["New Hampshire", "New Hampshire", "New Hampshire"],
["New Hampshire", "New Hampshire", "New Hampshire"],
["New Hampshire", "New Hampshire", "New Hampshire"]
]
...i.e. the connecting_stations
attribute of all of my Station
instances have the same "New Hampshire"
repeated 3 times.
But what I really wanted to do is:
>>> print(stations[0].connecting_stations)
["New Hampshire"]
>>> print(stations[1].connecting_stations)
["New Hampshire"]
>>> print(stations[2].connecting_stations)
["New Hampshire"]
...i.e. I want each one of my Station
instances to have one -- only one -- connecting station, which is called "New Hampshire"
, but actually they're getting 3 "New Hampshire"
strings each.
How is that even possible? What is happening and how can I solve my problem?
Actual full code:
import random
import turtle
class Game():
def __init__(self, difficulty):
number_of_stations = {
"easy": 3,
"medium": 5,
"hard": 8
}
turtle.setworldcoordinates(0, -500, 500, 0)
self.max_x = 500
self.min_y = -500
self.stations = self.create_stations(number_of_stations[difficulty])
self.create_connections()
def create_stations(self, number_of_stations):
available_colors = [
"#0A2463", "#3E92CC", "#C200FB", "#D8315B", "#1E1B18",
"#564138", "#EEC643", "#F46036"
]
available_xs = list(range(int(self.max_x*0.1), int(self.max_x*0.9)+1))
available_ys = list(range(int(self.min_y*0.9), int(self.min_y*0.1)+1))
stations = []
# Adding stations (random color and random pos)
for n in range(number_of_stations):
station_color = available_colors.pop(random.randint(0, len(
available_colors)-1))
station_x = available_xs.pop(
random.randint(0, len(available_xs)-1))
station_y = available_ys.pop(
random.randint(0, len(available_ys)-1))
for x in range(station_x-int(self.max_x*0.05),
station_x+int(self.max_x*0.05)):
if x in available_xs:
available_xs.remove(x)
for y in range(station_y+int(self.min_y*0.05),
station_y-int(self.min_y*0.05)):
if y in available_ys:
available_ys.remove(y)
stations.append(Station(n, station_color, (station_x, station_y)))
return stations
def create_connections(self):
for station in self.stations:
station.connecting_stations.append("New Hampshire")
class Station():
def __init__(self, identification,
color, position, connecting_stations=[]):
self.id = identification
self.color = color
self.position = position
self.x = self.position[0]
self.y = self.position[1]
self.connecting_stations = connecting_stations
self.number_of_connections = len(self.connecting_stations)
# Station's turtle instance setup
self.turtle_instance = turtle.Turtle()
self.turtle_instance.color(self.color)
self.turtle_instance.speed("fastest")
game1 = Game("hard")
for station in game1.stations:
print(station.id, station.connecting_stations)
Note: Sorry for the bad title. Did not find better and shorter words to describe my problem.