在 Python 中创建列表对象类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17461833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create List Object Class in Python
提问by arnobpl
It is a basic question. I am trying the following code:
这是一个基本问题。我正在尝试以下代码:
class SMS_store:
def __init__(self):
self=[] #probably something is wrong here
def add_new_arrival(self,from_number,time_arrived,text_of_SMS):
self.append([False,from_number,time_arrived,text_of_SMS]) #append list to self list
self[len(self)-1]=tuple(self[len(self)-1])
def message_count(self):
return len(self)
my_inbox=SMS_store()
my_inbox.add_new_arrival('01234','9:37 AM','How are you?')
But I get the following error:
但我收到以下错误:
>>>
Traceback (most recent call last):
File "C:\Users\Arnob\Desktop\New Text Document.py", line 15, in <module>
my_inbox.add_new_arrival('01234','9:37 AM','How are you?')
File "C:\Users\Arnob\Desktop\New Text Document.py", line 8, in add_new_arrival
self.append([False,from_number,time_arrived,text_of_SMS]) #append list to self list
AttributeError: 'SMS_store' object has no attribute 'append'
>>>
What is wrong in my code?
我的代码有什么问题?
采纳答案by John La Rooy
You can subclass listlike this
你可以list像这样子类化
class SMS_store(list):
def add_new_arrival(self, from_number, time_arrived, text_of_SMS):
self.append((False, from_number, time_arrived, text_of_SMS)) #append tuple to self
def message_count(self):
return len(self)
Notice there is no need for __init__unless you wish to do something extra there.
请注意,__init__除非您想在那里做一些额外的事情,否则没有必要。
You don't need to append a list and then turn it into a tuple, you can create the tuple directly with ()instead of []
你不需要附加一个列表然后把它变成一个元组,你可以直接用()而不是创建元组[]
回答by TerryA
You need to still create a normal variable name, just put self.as a prefix:
您仍然需要创建一个普通的变量名,只需self.作为前缀:
self.mylist = []
To access it, you do something like:
要访问它,您可以执行以下操作:
self.mylist.append(n)
Or:
或者:
self.mylist[3] = 'hi'
You're actually overriding self. You don't want to do that.
你实际上是覆盖self. 你不想那样做。
回答by NPE
If you want to inherit from list, use the following:
如果要继承自list,请使用以下命令:
class SMS_store(list):
^^^^^^
and remove that assignment to selffrom the __init__method.
并self从__init__方法中删除该赋值。
That said, you might want to simply have a named attribute containing the list:
也就是说,您可能只想拥有一个包含列表的命名属性:
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?')
As far as representing actual messages, this sounds like a good use case for namedtuple. It's just like a tuple, but allows access to fields by name. Here is a quick illustration:
就表示实际消息而言,这听起来像是namedtuple. 它就像一个元组,但允许按名称访问字段。这是一个快速说明:
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

