Python Starter Design Question

I am trying to find the best way to create a couple of classes. I'm new to Python (and OOP in general) and just want to make sure I'm doing it right. I have two classes: "Users" and "User".

class User(object): def __init__(self): pass class Users(object): def __init__(self): self.users = [] def add(self, user_id, email): u = User() u.user_id = user_id u.email = email self.users.append(u) users = Users() users.add(user_id = 1, email = 'bob @example.com ') 

If I want to get my users, I use:

 for u in users.users: print u.email 

"users.users" seems a bit redundant. Am I doing it right?

+6
python iterator oop
source share
5 answers

Put this in your Users :

 def __iter__(self): return iter(self.users) 

Now you can:

 for u in users: print u.email 

Docs

+11
source share

I would say, really. Your Users class seems to be just a list of users, so I would just make it a list, not an entire class. Here is what I will do:

 class User(object): def __init__(self, user_id=None, email=None): self.user_id, self.email = user_id, email users = [] users.append(User(user_id = 1, email = ' bob@example.com ')) for u in users: print u.email 

If you want Users be a class for some other reason, you could inherit it from list or (if not), you can add them to the definition:

 class Users(object): # rest of code def __iter__(self): return iter(self.users) 

So you can just say:

 users = Users() ... for u in users: print u.email 
+17
source share

You probably just need a list of user objects, not a class that contains multiple users.

 class User(object): def __init__(self, user_id, email): self.user_id = user_id self.email = email users = [] users.append(User(user_id = 1, email = ' bob@example.com ')) 

All member attributes for the user must be in the User class, and not in the Users class.

+6
source share

I don't see anything wrong with users.users, but if you prefer a more convenient way to do this, you can override __iter__ to "Users".

 class Users(object): def __init__(self): self.users = [] def add(self, user_id, email): u = User() u.user_id = user_id u.email = email self.users.append(u) def __iter__(self): return iter(self.users) 

Now you can do this:

 for u in users: print u.email 

The special __iter__ method makes your object behave like an iterator

+4
source share

There is no β€œblack” and β€œwhite”, only shades of gray. You do not need a special Users class if it is just a list.

Another way:

 class User: all_users = [] def __init__(self, id, email): self.id = id # No need to call it user_id - it a User object, after all! self.email = email self.all_users.append(self) #automatically add to list of all users def __str__(self): return '%s(%s)' % (self.id, self.email) 

Then, if you typed the above value into user.py :

  >>> from user import *
 >>> bob = User ('bob', ' bob@test.com ')
 >>> alice = User ('alice', ' alice@test.com ')
 >>> for u in User.all_users:
 ... print u
 ...
 bob ( bob@test.com )
 alice ( alice@test.com )
 >>>

Just an example so you can think.

+1
source share

All Articles