Generate a new value everytime I call the variable

Question:

I’m tweaking faker, a module in python which generates random fake names address etc.

class us:
    def __init__(self):
        fake = Faker('en_US')
        self.name = fake.name()
        self.fname = fake.first_name()
        self.lname = fake.last_name()
        self.street = fake.street_address()
        self.city = fake.city()
        self.state = fake.state()
        self.abbr = fake.state_abbr()
        self.zip = fake.postalcode()
        self.phone = fake.msisdn()
        self.ua = fake.user_agent()
        self.email = fake.email(1, 'gmail.com')
        self.password = fake.password()

when I loop it the same values came :

Diane Gordon
Diane Gordon
Diane Gordon
Diane Gordon
Diane Gordon

name = us().name
for x in range(0,5):
    print(name)

I wonder if I can generate different values when calling variable "name"

Asked By: renea

||

Answers:

Each call to faker.(whatever) generates a new value, so you should use properties instead:

class us:
    def __init__(self):
        self._faker = Faker('en_US')

    @property
    def name(self):
        return self._faker.name()

    # and so on...
u = us()

for _ in range(5):
    print(u.name)
Answered By: Iguananaut
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.