Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9088519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:55:21+00:00 2026-06-16T21:55:21+00:00

I wrote this piece of code and I can’t figure out what I exactly

  • 0

I wrote this piece of code and I can’t figure out what I exactly did wrong. I create two instances of the ‘Route’ class, and somehow they share the value for the ‘coords’ list.

import random

class Route():
    def __init__(self):
        self.coords = []  
        self.distance = 0
    def Generate(self, cities):
        random.shuffle(cities)
        self.coords = cities

class Citymap():
    def __init__(self, nr):
        self.Nr_of_cities = nr
        self.cities = []
    def Generate_map(self):
        for i in range(0, self.Nr_of_cities):
            self.cities.append((random.randint(0, 750), random.randint(0, 750)))


city = Citymap(6)
city.Generate_map()

a = Route()
a.Generate(city.cities)

b = Route()
b.Generate(city.cities)

print a.coords
print b.coords

The output of a and b:

[(429, 713), (336, 611), (555, 465), (397, 227), (222, 412), (491, 322)]
[(429, 713), (336, 611), (555, 465), (397, 227), (222, 412), (491, 322)]

a and b should be different instances but somehow they end up sharing the ‘coords’ variable. Could someone help me out?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:55:22+00:00Added an answer on June 16, 2026 at 9:55 pm

    You have to clone the city.cities list or it will just be shared between them :

    a = Route()
    a.Generate(city.cities[:])
    
    b = Route()
    b.Generate(city.cities[:])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this piece of code that is supposed to redirect something written on
I've wrote this simple piece of code. And I have a slight problem with
I have a program I wrote in Windows with this piece of code that
I wrote this code for zoom in/out . it works but even with one
I wrote this code for zoom in / out and it suppesed to only
I wrote this piece of code to my bootstrap public function _initRouter() { $pages
I wrote this piece of code, it's obviously flawed. How do I go about
I wrote this small piece of code in C to test memcmp() strncmp() strcmp()
I recently wrote a piece of code that looked a bit like this: IEnumerable<DTO.Employee>
Hey, for this piece of code, the person who wrote the system communicates data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.