Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Generate a new value everytime I call the variable

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading