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 204123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:26:23+00:00 2026-05-11T17:26:23+00:00

(Edit: randrange is just random.randrange, I didn’t write my own RNG) I’m trying to

  • 0

(Edit: randrange is just random.randrange, I didn’t write my own RNG)

I’m trying to create a list of instances of a class I defined. Here’s the entire class (by request):

from random import randrange

class Poly:
    points = [0] * 8
    fill = 'red'
    alpha = 1.0

    def __init__(self, width=100, height=100):
        for i in range(0, 8, 2):
            self.points[i] = randrange(width)
            self.points[i+1] = randrange(height)
        self.alpha = random()
        return

Seems to work fine:

>>> for i in range(5):
        Poly().points

[28, 64, 93, 26, 15, 31, 44, 50]
[24, 14, 47, 14, 35, 17, 63, 62]
[99, 28, 90, 29, 56, 59, 57, 33]
[62, 56, 48, 28, 40, 73, 70, 99]
[99, 32, 27, 99, 42, 57, 86, 12]

But if I try to create a list of these objects, I get separate instances (different memory addresses) but they all have the same random values:

>>> p = []
>>> for i in range(5):
        p.append(Poly())

>>> p
[<gen_image.Poly instance at 0x02D773C8>, <gen_image.Poly instance at 0x02D77FD0>, <gen_image.Poly instance at 0x0321D030>, <gen_image.Poly instance at 0x02D51E40>, <gen_image.Poly instance at 0x02D51DA0>]

>>> for poly in p:
        print poly.points

[75, 18, 5, 76, 6, 64, 95, 54]
[75, 18, 5, 76, 6, 64, 95, 54]
[75, 18, 5, 76, 6, 64, 95, 54]
[75, 18, 5, 76, 6, 64, 95, 54]
[75, 18, 5, 76, 6, 64, 95, 54]

What’s going on here? And what’s the right way to do what I’m trying to do?

  • 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-05-11T17:26:23+00:00Added an answer on May 11, 2026 at 5:26 pm

    Move the creation of the array into the __init__ method.

    You’re working with a shared array among all objects.

    The reason the first shows different is that you print the contents of that array before you construct a new Poly object and thus trample over the array contents. If you had kept them around and inspected them later they would all appear to have the same contents as the last one you generated.

    Oh, and try not to simplify code when posting questions. Always post complete, but short, programs that reproduce the problem.

    Here’s a short, but complete, program that demonstrates the problem you’re having:

    from random import randrange
    class Poly:
        points = [0]*8
    
        def __init__(self, width=100, height=100):
            for i in range(0, 8, 2):
                self.points[i] = randrange(width)
                self.points[i+1] = randrange(height)
            return
    
    p1 = Poly()
    print "p1:", p1.points
    p2 = Poly()
    print "p2:", p2.points
    print "p1:", p1.points
    

    Sample output:

    [C:\Temp] test.py
    p1: [19, 5, 1, 46, 93, 18, 18, 57]
    p2: [92, 71, 42, 84, 54, 29, 27, 71]
    p1: [92, 71, 42, 84, 54, 29, 27, 71]
    

    Notice how p1 changed.

    The fixed code could be as simple as:

    from random import randrange
    class Poly:
        def __init__(self, width=100, height=100):
            self.points = [0]*8
            for i in range(0, 8, 2):
                self.points[i] = randrange(width)
                self.points[i+1] = randrange(height)
            return
    

    although I prefer the append variant that @Doug posted here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
Why is my program giving me an error here? import random TheNumber = random.randrange(1,200,1)
Edit: This question was written in 2008, which was like 3 internet ages ago.
Edit: From another question I provided an answer that has links to a lot
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT: This question is more about language engineering than C++ itself. I used C++
EDIT What small things which are too easy to overlook do I need to
Edit : Solved, there was a trigger with a loop on the table (read
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code

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.