子类化内置类型的样式指南? [英] Style guide for subclassing built-in types?
问题描述
请参阅以下代码:
--------------------------------
class rev_wrap(object):
def __init __(self,l):
self.l = l
def __getitem __(self,i):
返回self.l [-i-1]
class rev_subclass(list):
def __getitem __(self,i):
返回列表.__ getitem __(self,-i-1)
if __name __ ==''__ main__' ':
l = rev_wrap([1,2,3])
断言l [0] == 3
断言列表(l) == [3,2,1]
l = rev_subclass([1,2,3])
断言l [0] == 3
断言列表(l)== [3,2,1]
我知道有反转的和反向但那不是重点。
代码在最后一行失败。
现在不推荐使用UserList(不推荐)我认为子类化
built-类型比包装它们更好。我如何正确地改变内置类型的行为?b $ b?我想我必须覆盖
__iter__然后接下来传递最后一行。这是一种好风格吗?如果是这样,
最推荐的实施方式是什么?
提前谢谢。
Jane
Please see the following code:
--------------------------------
class rev_wrap(object):
def __init__(self,l):
self.l=l
def __getitem__(self,i):
return self.l[-i-1]
class rev_subclass(list):
def __getitem__(self,i):
return list.__getitem__(self,-i-1)
if __name__==''__main__'':
l=rev_wrap([1,2,3])
assert l[0]==3
assert list(l)==[3,2,1]
l=rev_subclass([1,2,3])
assert l[0]==3
assert list(l)==[3,2,1]
I know there is "reversed" and "reverse" but that''s not the point. The
code fails at the last line.
Now that UserList is deprecated(not recommended) I suppose subclassing
built-in types are preferable than wrapping them. How do I properly
change the behaviours of built-in types? I think I have to override
__iter__ and next to pass the last line. Is it a good style? If so,
what is the most recommended way of implementing them?
Thank you in advance.
Jane
推荐答案
Jane Austine写道:
Jane Austine wrote:
请参阅以下代码:
------ --------------------------
class rev_wrap(object):
def __init __(self,l):
self.l = l
def __getitem __(self,i):
返回self.l [-i-1]
类rev_subclass(list):
def __getitem __(self,i):
返回列表.__ getitem __(self,-i-1)
如果__name __ ==''__ main__'':
l = rev_wrap ([1,2,3])
断言l [0] == 3
断言列表(l)== [3,2,1]
l = rev_subclass ([1,2,3])
断言l [0] == 3
断言列表(l)== [3,2,1]
Please see the following code:
--------------------------------
class rev_wrap(object):
def __init__(self,l):
self.l=l
def __getitem__(self,i):
return self.l[-i-1]
class rev_subclass(list):
def __getitem__(self,i):
return list.__getitem__(self,-i-1)
if __name__==''__main__'':
l=rev_wrap([1,2,3])
assert l[0]==3
assert list(l)==[3,2,1]
l=rev_subclass([1,2,3])
assert l[0]==3
assert list(l)==[3,2,1]
>
哦......我忘了一个。断言l == [3,2,1]此时也没有通过
。 print l输出错误的([1,2,3])。
Oh... I forgot one. assert l==[3,2,1] at this point doesn''t pass
either. "print l" outputs the wrong one([1,2,3]) as well.
我猜打印是使用lsit的__repr __(或__str__?)方法 -
你也需要覆盖。
问候,
模糊
< a rel =nofollowhref =http://www.voidspace.org.uk/python/index.shtmltarget =_ blank> http://www.voidspace.org.uk/python/index.shtml
I guess print is using the __repr__ (or __str__ ?) methods of lsit -
which you will need to override as well.
Regards,
Fuzzy
http://www.voidspace.org.uk/python/index.shtml
Fuzzyman写道:
Fuzzyman wrote:
我想print是使用lsit的__repr __(或__str__?)方法 -
你也需要覆盖它。
问候,
模糊
http://www.voidspace.org.uk/python/index.shtml
I guess print is using the __repr__ (or __str__ ?) methods of lsit -
which you will need to override as well.
Regards,
Fuzzy
http://www.voidspace.org.uk/python/index.shtml
谢谢,但问题是我必须在
重复的地方表达我的意图 - __iter __(以及next),_ _ ttr __,__ eq__等等/>
on。
p.s.我之所以不坚持逆转甚至逆转:假设
列表的大小很大。
Thank you but the problem is that I have to express my intention in
duplicate places -- __iter__(along with "next"), __str__, __eq__ and so
on.
p.s. the reason I''m not sticking to reversed or even reverse : suppose
the size of the list is huge.
这篇关于子类化内置类型的样式指南?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!