I have spent a good while searching through this website so I hope this question hasn't been asked before - apologises if it has. I'm learning classes for the first time and I'm making a class with multiple users (could be 50+ but for now, I just have 2 in my example). What I'm trying to do is have certain information about users/employees and be able to print them all in one go... in a way that isn't a complete eyesore! This is what I have attempted:
class User:
def __init__(self, user_id, first, last, address):
self.user_id = user_id
self.first = first
self.last = last
self.address = address
self.email = first + '.' + last + '@python.com'
def all_users(self):
print()
print('User ID: {} First Name: {} {} {} {}'.format(self.user_id, self.first, self.last, self.address, self.email))
print()
user_1 = User(123, 'Kim', 'N', 'London')
user_2 = User(312, 'Chris', 'E', 'Japan')
print(all_users(User))
This is the error message that I am receiving:
print('User ID: {} First Name: {} {} {} {}'.format(self.user_id, self.first, self.last, self.address, self.email))
AttributeError: type object 'User' has no attribute 'user_id'
Thanks in advance for any help or guidance.
Useris a class, not an object. You've confused the two in your program.user_1anduser_2have the attributes;Userdoes not.all_users()is a very misleading method name, as it only prints information for one user.