'Creating copy of class instance with randomly init variable (python)

I need to initialize an instance of a class with a randomly generated number. I then need to use this instance in several different trials. However, I am having troubles making exact copies of this instance - the randomly generated number will not stay the same. Each copy is generating a new random number.

temp = proc(random.randint(min, max))
tempCpy = copy.copy(temp) #changes on temp interfere with tempCpy
tempCpy = copy.deepcopy(temp)  #doesn't start with same random number


Solution 1:[1]

You have a class something like this, I assume:

class proc(object):

    def __init__(self, value):
        self.value = value

And when you initialize your first instance, you write:

temp = proc(random.randint(min, max))

And this stores the random number in temp.value.

To make a copy, you can just pass in temp.value to the proc constructor:

dupe = proc(temp.value)

You could also write your class to do this automatically, so that when you pass an instance of proc to the proc constructor, it makes a copy (as with other types, such as list and dict, whose constructors can be used to make copies of them). This has the advantage of keeping all the knowledge of what needs to be done to make copies in the class instead of the caller needing to know where the data is stored.

class proc(object):
    def __init__(self, value):
        if isinstance(value, proc):
            value = value.value
        self.value = value

Now, you can write:

dupe = proc(temp)

A related method is to write an alternate constructor (often named from) as a classmethod on the class.

class proc(object):

    def __init__(self, value):
        self.value = value

    @classmethod
    def from(cls, other):
        return cls(other.value)

Copying is then:

dupe = proc.from(temp)

Alternatively, you could add a method to proc to make the copy:

class proc(object):

    def __init__(self, value):
        self.value = value

    def copy(self):
        return type(self)(self.value)

Now your copy is:

dupe = temp.copy()

Finally, if you name your copy method __copy__ instead (or use both names), it will magically work with copy.copy!

class proc(object):

    def __init__(self, value):
        self.value = value

    def copy(self):
        return type(self)(self.value)

    __copy__ = copy   # alias for use by 'copy' module

temp = proc(random.randint(min, max))
dupe = copy.copy(temp)

Solution 2:[2]

You need to initialize random.seed() with an argument you will reuse. It will guarantee that you generate "the same random number". (e.g. random.seed(0) before the rest of your code)

Solution 3:[3]

Perhaps, you can just make random number separatly from your object?

init_numb = random.randint(min, max)
temp = proc(init_numb)
tempCpy = copy.copy(temp) #changes on temp interfere with tempCpy
tempCpy = copy.deepcopy(temp)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 vermillon
Solution 3 Mikhail