0

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>]']
2
  • 3
    Your objects are not printable, you should look at the function __ str __ to make it printable Commented Apr 22, 2022 at 12:59
  • What's the point of trying to duplicate an existing list? Are you sure this is what is being requested? Is not rather to generate a list of User objects from a list of the parameters? Commented Apr 22, 2022 at 12:59

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

the thing is that i want to add the users instanciated it in another list (it's an exercise in formation)
2

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)

1 Comment

can you show me the output please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.