2

I am trying to make a python class that keeps a list of uniformly distributed random numbers per class but the numbers must be unique to that class. However the list in each new object is copied exactly as is from the first object and if I manually append new values to the contents of the second objects list, the same change is applied to the first object's list and for every new class I create the list is again copied and it appends however many values I specified.

The code is as follows:

import random as random

class myClass:
    myRandomList = []

    def __init__(self, lengthOfList):
        for i in range(0,lengthOfList):
            self.myRandomList.append(random.uniform(-1.0,1.0))

a = myClass(2) #make a list with 2 random numbers
b = myClass(1) #make a list with 1 random number
print(a.myRandomList)
print(b.myRandomList)

The output of the above code gives the following:

[0.3640932307078235, 0.8858169279430881, 0.18712932281033723]
[0.3640932307078235, 0.8858169279430881, 0.18712932281033723]

When it should be giving something like this rather:

[0.3640932307078235, 0.8858169279430881]
[0.18712932281033723]

How can I make it so that when a new class is created it creates an entirely new list of values for that class only without copying the list from a previously instantiated object and without affecting previous object's lists?

1
  • 1
    A class is a blueprint for an object. Your language is confusing because you're using class synonymously with object. You should edit your post for clarification. Commented Sep 6, 2018 at 10:46

3 Answers 3

2

I think what you want is for every instance of a class (i.e. a and b in your code) to have its own list of random numbers, matching the length you specified when you created the instance. To do this, you should set self.myRandomList within init. Here's the change to your original code:

import random as random

class myClass:

  def __init__(self, lengthOfList):
    self.myRandomList = []
    for i in range(0,lengthOfList):
      self.myRandomList.append(random.uniform(-1.0,1.0))

a = myClass(2) #make a list with 2 random numbers
b = myClass(1) #make a list with 1 random number
print(a.myRandomList)
print(b.myRandomList)

What you were doing before was setting myRandomList as a class variable, which will always be shared by all instances of the class, rather than an instance variable. See here for more details: https://www.digitalocean.com/community/tutorials/understanding-class-and-instance-variables-in-python-3

Hope that helps!

Sign up to request clarification or add additional context in comments.

3 Comments

Yours had the better explanation, have my +1
Each object must have it's own unique set of random numbers. Thus, the init method needs to reference a class level set to check that the number has not already been issued to another object.
This is definitely the solution I was looking for after struggling overnight with this issue, thank you! Upvoted :)!
1

Simply declaring self.myRandomList in __init__, rather than in the class declaration does the trick.

class myClass:
    def __init__(self, lengthOfList):
        self.myRandomList = []
        for i in range(0,lengthOfList):
            self.myRandomList.append(random.uniform(-1.0,1.0))

Comments

0

If you want to ensure that each object has unique numbers across all instances then you'll need to have each instance check the generated values against a class level set to ensure uniqueness.

import random as random

class MyClass:

    class_random_set = set()

    def __init__(self, lengthOfList):
        self.my_random_set = set()
        for _ in range(0, lengthOfList):
            while True:
                num = random.uniform(-1.0, 1.0)
                if num not in self.class_random_set:
                    self.class_random_set.add(num)
                    self.my_random_set.add(num)
                    break


a = MyClass(2) #make a set with 2 random numbers
b = MyClass(1) #make a set with 1 random number
print(a.my_random_set)
print(b.my_random_set)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.