Home > OS >  python list sending odd results
python list sending odd results

Time:04-23

for learning, i had to create a class User, use that class to create objects and finally add them to another list via a for loop. but the result sent are unexpected and totally different from waht i wanted. here's the class:

class User:
def __init__(self, firstname : str='', lastname: str = '', email: str='', newsletter : boolean = False):
    self.firstname = firstname,
    self.lastname = lastname,
    self.email = email,
    self.newsletter = newsletter

and here are the objects:

new_users = [
User('Joe', 'Dalton', '[email protected]', True),
User('William', 'Dalton', '[email protected]'),
User('Jack', 'Dalton', '[email protected]'),
User('Averell', 'Dalton', '[email protected]', True)

]

the loop is right here:

users = []
for i in range (len(new_users)):
    users.append(str(new_users))
    print(users)

here's what i have in the terminal:

['[<__main__.User object at 0x7f4c363da1f0>, <__main__.User object at    0x7f4c363c2f10>, <__main__.User object at 0x7f4c3639b9a0>, <__main__.User object at 0x7f4c36326250>]', '[<__main__.User object at 0x7f4c363da1f0>, <__main__.User object at 0x7f4c363c2f10>, <__main__.User object at 0x7f4c3639b9a0>, <__main__.User object at 0x7f4c36326250>]', '[<__main__.User object at 0x7f4c363da1f0>, <__main__.User object at 0x7f4c363c2f10>, <__main__.User object at 0x7f4c3639b9a0>, <__main__.User object at 0x7f4c36326250>]', '[<__main__.User object at 0x7f4c363da1f0>, <__main__.User object at 0x7f4c363c2f10>, <__main__.User object at 0x7f4c3639b9a0>, <__main__.User object at 0x7f4c36326250>]']

CodePudding user response:

If you want to print an object in a format that's understandable/meaningful to the reader then you should implement the __str__() function.

Also, it looks like you're misunderstanding how you would iterate over your list to print the values.

Perhaps this will make things clearer:

class User:
    def __init__(self, firstname='', lastname='', email='', newsletter=False):
        self.firstname = firstname
        self.lastname = lastname
        self.email = email
        self.newsletter = newsletter
    def __str__(self):
        return f'{self.firstname=}, {self.lastname=}, {self.email=}, {self.newsletter=}'.replace('self.', '')

new_users = [
    User('Joe', 'Dalton', '[email protected]', True),
    User('William', 'Dalton', '[email protected]'),
    User('Jack', 'Dalton', '[email protected]'),
    User('Averell', 'Dalton', '[email protected]', True)]

for user in new_users:
    print(user)

Output:

firstname='Joe', lastname='Dalton', email='[email protected]', newsletter=True
firstname='William', lastname='Dalton', email='[email protected]', newsletter=False
firstname='Jack', lastname='Dalton', email='[email protected]', newsletter=False
firstname='Averell', lastname='Dalton', email='[email protected]', newsletter=True

CodePudding user response:

While iterating objects you are adding it wrong. Also remove the comma in the constructor variable assignments. str method makes the printing readable It should be like below:

class User:
    def __init__(self, firstname : str='', lastname: str = '', email: str='', newsletter:bool = False):
        self.firstname = firstname
        self.lastname = lastname
        self.email = email
        self.newsletter = newsletter
        
    def __str__(self):
        return f"{self.firstname} {self.lastname}"
        
new_users = [
User('Joe', 'Dalton', '[email protected]', True),
User('William', 'Dalton', '[email protected]'),
User('Jack', 'Dalton', '[email protected]'),
User('Averell', 'Dalton', '[email protected]', True)]

users = []
for new_user in new_users:
    users.append(new_user)
    
for user in users:
    print(user)
  • Related