为什么Python不支持记录类型,即mutable namedtuple [英] Why Python does not support record type i.e. mutable namedtuple
问题描述
为什么Python本身不支持记录类型?这是一个可变的namedtuple版本的问题。
我可以使用 namedtuple._replace
。但我需要有一个集合中的这些记录,并且由于 namedtuple._replace
创建另一个实例,我还需要修改集合,变得混乱快。
背景:
我有一个设备的属性,我需要通过TCP / IP轮询它。即其表示是一个可变对象。
编辑:
我有一组设备需要轮询。
编辑:
我需要使用PyQt迭代显示其属性的对象。我知道我可以添加特殊的方法,如 __ getitem __
和 __ iter __
,但我想知道是否有更简单的方法。 / p>
编辑:
我想要的属性是固定的类型(就像它们在我的设备中一样),但是是可变的。
Python< 3.3
这是什么意思?
class Record(object):
__slots __ =attribute1,attribute2,attribute3,
def item(self):
dict style items
return [
(field_name,getattr(self,field_name))
for field_name in self .__ slots__]
def __iter __(self):
遍历字段tuple /列表样式
在自我的field_name中。$ slots $:
yield getattr(self,field_name)
def __getitem __(self,index):
tuple / list style getitem
return getattr(self,self .__ slots __ [index])
>> ; r = Record()
>>>> r.attribute1 =hello
>>>> r.attribute2 =there
>>>> r.attribute3 = 3.14
>>>> print r.items()
[('attribute1','hello'),('attribute2','there'),('attribute3',3.1400000000000001)]
>> print tuple(r)
('hello','there',3.1400000000000001)
Python≥3.3更新
您可以使用< a href =http://docs.python.org/py3k/library/types.html#types.SimpleNamespace> types.SimpleNamespace
:
>>>导入类型
>>>> r = types.SimpleNamespace()
>>>> r.attribute1 =hello
>>>> r.attribute2 =there
>>>> r.attribute3 = 3.14
dir(r)
将为您提供属性名称(过滤出所有 .startswith(__)
,当然)。
Why does not Python support a record type natively? It's a matter of having a mutable version of namedtuple.
I could use namedtuple._replace
. But I need to have these records in a collection and since namedtuple._replace
creates another instance, I also need to modify the collection which becomes messy quickly.
Background: I have a device whose attributes I need to get by polling it over TCP/IP. i.e. its representation is a mutable object.
Edit: I have a set of devices for whom I need to poll.
Edit:
I need to iterate through the object displaying its attributes using PyQt. I know I can add special methods like __getitem__
and __iter__
, but I am wondering whether there is any easier way.
Edit: I would prefer a type whose attribute are fixed (just like they are in my device), but are mutable.
Python <3.3
You mean something like this?
class Record(object):
__slots__= "attribute1", "attribute2", "attribute3",
def items(self):
"dict style items"
return [
(field_name, getattr(self, field_name))
for field_name in self.__slots__]
def __iter__(self):
"iterate over fields tuple/list style"
for field_name in self.__slots__:
yield getattr(self, field_name)
def __getitem__(self, index):
"tuple/list style getitem"
return getattr(self, self.__slots__[index])
>>> r= Record()
>>> r.attribute1= "hello"
>>> r.attribute2= "there"
>>> r.attribute3= 3.14
>>> print r.items()
[('attribute1', 'hello'), ('attribute2', 'there'), ('attribute3', 3.1400000000000001)]
>>> print tuple(r)
('hello', 'there', 3.1400000000000001)
Note that the methods provided are just a sample of possible methods.
Python ≥3.3 update
You can use types.SimpleNamespace
:
>>> import types
>>> r= types.SimpleNamespace()
>>> r.attribute1= "hello"
>>> r.attribute2= "there"
>>> r.attribute3= 3.14
dir(r)
would provide you with the attribute names (filtering out all .startswith("__")
, of course).
这篇关于为什么Python不支持记录类型,即mutable namedtuple的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!