jeudi 28 septembre 2017

Generating random number for class attribute

I have the following code:

from numpy import random

class Person():

    def __init__(self, name, age=random.randint(18,65)):
        self.name = name
        self.age = age

I want age to be a random number between 18 and 65, unless explicitly specified. However, when I create different instances of this class, like so:

p1 = Person('Bob')
p2 = Person('Sue')
p3 = Person('Jeff')

Each person always have the same age. How can I fix this?




Aucun commentaire:

Enregistrer un commentaire