擅长:python、mysql、java
<p>如果要从<code>list</code>继承,请使用以下命令:</p>
<pre><code>class SMS_store(list):
^^^^^^
</code></pre>
<p>并从<code>__init__</code>方法中删除对<code>self</code>的赋值。</p>
<p>也就是说,您可能只需要一个包含列表的命名属性:</p>
<pre><code>class SMS_store(object):
def __init__(self):
self.messages = []
def add_new_arrival(self, from_number, time_arrived, text_of_SMS):
self.messages.append((False,from_number,time_arrived,text_of_SMS))
def message_count(self):
return len(self.messages)
my_inbox = SMS_store()
my_inbox.add_new_arrival('01234','9:37 AM','How are you?')
</code></pre>
<p>就表示实际消息而言,这听起来是<a href="http://docs.python.org/2/library/collections.html#collections.namedtuple" rel="nofollow">^{<cd4>}</a>的一个很好的用例。它就像元组,但允许按名称访问字段。下面是一个简单的例子:</p>
<pre><code>import collections
SMS = collections.namedtuple('SMS', 'from_number time_arrived text_of_SMS')
sms = SMS(from_number='01234', time_arrived='9:37 AM', text_of_SMS='How are you?')
print sms.text_of_SMS
</code></pre>