Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python newbie class design question

I'm trying to figure out the best way to design a couple of classes. I'm pretty new to Python (and OOP in general) and just want to make sure that I'm doing this 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 = '[email protected]')

If I want to retrieve my users, I use:

for u in users.users:
    print u.email

"users.users" seems to be a bit redundant. Am I doing this right?

like image 657
jamieb Avatar asked Jan 20 '10 17:01

jamieb


5 Answers

I would say not really. Your Users class appears to just be a list of users, so I would just make it a list rather than a whole class. Here's what I would 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 = '[email protected]'))

for u in users:
    print u.email

If you want Users to be a class of its own for some other reason, you could have it inherit from list, or (if not) you could add these to the definition:

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

That way, you can simply say:

users = Users()
...
for u in users:
    print u.email
like image 100
Chris Lutz Avatar answered Nov 12 '22 23:11

Chris Lutz


Put this in your Users:

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

Now you can:

for u in users:
    print u.email

Docs

like image 24
recursive Avatar answered Nov 12 '22 22:11

recursive


You probably just want a list of User objects rather than a class which 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 = '[email protected]'))

All member attributes for User should reside in the User class, not the Users class.

like image 27
cquillen Avatar answered Nov 12 '22 21:11

cquillen


I don't see anything wrong with users.users, but if you prefer a nicer way to do it you could override __iter__ in 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 __iter__ special method makes your object behave as an iterator

like image 4
Nadia Alramli Avatar answered Nov 12 '22 23:11

Nadia Alramli


There's no "black" and "white" here, just shades of grey. You don't need a special Users class if it's just going to be a list.

Another way:

class User:
    all_users = []

    def __init__(self, id, email):
        self.id = id # No need to call it user_id - it's 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 into user.py:

>>> from user import *
>>> bob = User('bob', '[email protected]')
>>> alice = User('alice', '[email protected]')
>>> for u in User.all_users:
...     print u
...
bob([email protected])
alice([email protected])
>>>

Just an example to get you thinking.

like image 1
Vinay Sajip Avatar answered Nov 12 '22 21:11

Vinay Sajip