English 中文(简体)
甲型新娘阶级设计问题
原标题:Python newbie class design question

我试图指出设计两门课程的最佳途径。 我向沙尔(和奥巴普组织一般)说来,只是想确保我这样做。 我有两个班子:“用户”和“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 )

如果我想找回我的用户,我就使用:

for u in users.users:
    print u.email

“用户.用户”似乎是多余的。 我这样做了吗?

最佳回答

页: 1

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

现在你们可以:

for u in users:
    print u.email

Docs/a>

问题回答

我不会说真的。 <代码>Users 类别似乎只是用户名单,因此我只是将其列为一个清单,而不是一个整类。 在此,我要做的是:

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

如果您希望<代码>Users成为其本人的一类,出于某种其他原因,你可以继承<代码>>list/code>,或者(如果没有)你可以添加这些定义:

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

这样,你就可以说:

users = Users()
...
for u in users:
    print u.email

你可能只想一份用户物体清单,而不是一个包含多个用户的类别。

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 ))

用户的所有成员属性都应居住在用户类别,而不是用户类别。

我看不出用户的任何错误,但如果你更喜欢更nic的方法,你可以凌驾于

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)

现在你可以这样做:

for u in users:
    print u.email

<>代码>iter__ 特殊方法使你的物体成为探测器

这里没有“黑色”和“黑白”,只是灰尘。 页: 1 用户类别如果只是一个清单的话。

另一种方式:

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)

然后,如果您将以上分类为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)
>>>

了解你的榜样。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签