1. 序列__getitem__
如果没有 __iter__ 和 __contains__ 方法,
python 会调用 __getitem__ 方法,
设法让 迭代 和 in 运算符可用
1
2
3
4
5
6
7
8
9
10
11
|
class foo:
def __getitem__( self , pos):
return range ( 0 , 30 , 10 )[pos]
f = foo()
print (f[ 1 ]) # 10
for i in f:
print (i) # 0, 10, 20
# 如果没有 __iter__ 和 __contains__ 方法,
# python 会调用 __getitem__ 方法,
# 设法让迭代和 in 运算符可用
|
2. __setitem__
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import collections
card = collections.namedtuple( 'card' , [ 'rank' , 'suit' ])
class frenchdeck:
ranks = [ str (n) for n in range ( 2 , 11 )] + list ( 'jqka' )
suits = 'spades diamonds clubs hearts' .split()
def __init__( self ):
self ._cards = [card(rank, suit) for suit in self .suits for rank in self .ranks]
def __len__( self ):
return len ( self ._cards)
def __getitem__( self , position):
return self ._cards[position]
def __str__( self ):
return "," .join( str (i) for i in self ._cards)
from random import shuffle
deck = frenchdeck()
print ( str (deck))
# card(rank='2', suit='spades'),card(rank='3', suit='spades'),card(rank='4', suit='spades'),card(rank='5', suit='spades'),card(rank='6', suit='spades'),card(rank='7', suit='spades'),card(rank='8', suit='spades'),card(rank='9', suit='spades'),card(rank='10', suit='spades'),card(rank='j', suit='spades'),card(rank='q', suit='spades'),card(rank='k', suit='spades'),card(rank='a', suit='spades'),card(rank='2', suit='diamonds'),card(rank='3', suit='diamonds'),card(rank='4', suit='diamonds'),card(rank='5', suit='diamonds'),card(rank='6', suit='diamonds'),card(rank='7', suit='diamonds'),card(rank='8', suit='diamonds'),card(rank='9', suit='diamonds'),card(rank='10', suit='diamonds'),card(rank='j', suit='diamonds'),card(rank='q', suit='diamonds'),card(rank='k', suit='diamonds'),card(rank='a', suit='diamonds'),card(rank='2', suit='clubs'),card(rank='3', suit='clubs'),card(rank='4', suit='clubs'),card(rank='5', suit='clubs'),card(rank='6', suit='clubs'),card(rank='7', suit='clubs'),card(rank='8', suit='clubs'),card(rank='9', suit='clubs'),card(rank='10', suit='clubs'),card(rank='j', suit='clubs'),card(rank='q', suit='clubs'),card(rank='k', suit='clubs'),card(rank='a', suit='clubs'),card(rank='2', suit='hearts'),card(rank='3', suit='hearts'),card(rank='4', suit='hearts'),card(rank='5', suit='hearts'),card(rank='6', suit='hearts'),card(rank='7', suit='hearts'),card(rank='8', suit='hearts'),card(rank='9', suit='hearts'),card(rank='10', suit='hearts'),card(rank='j', suit='hearts'),card(rank='q', suit='hearts'),card(rank='k', suit='hearts'),card(rank='a', suit='hearts')
shuffle(deck) # typeerror: 'frenchdeck' object does not support item assignment
|
加入__setitem__
1
2
|
def __setitem__( self , key, value):
self ._cards[key] = value
|
1
2
3
4
|
shuffle(deck)
print ( str (deck))
# 牌被随机打乱了
# card(rank='6', suit='hearts'),card(rank='2', suit='spades'),card(rank='3', suit='hearts'),card(rank='a', suit='clubs'),card(rank='7', suit='clubs'),card(rank='q', suit='diamonds'),card(rank='5', suit='hearts'),card(rank='6', suit='spades'),card(rank='5', suit='diamonds'),card(rank='8', suit='diamonds'),card(rank='k', suit='hearts'),card(rank='q', suit='hearts'),card(rank='8', suit='hearts'),card(rank='3', suit='diamonds'),card(rank='q', suit='clubs'),card(rank='a', suit='diamonds'),card(rank='9', suit='hearts'),card(rank='7', suit='diamonds'),card(rank='a', suit='spades'),card(rank='j', suit='hearts'),card(rank='3', suit='clubs'),card(rank='4', suit='hearts'),card(rank='q', suit='spades'),card(rank='10', suit='diamonds'),card(rank='10', suit='hearts'),card(rank='k', suit='diamonds'),card(rank='2', suit='diamonds'),card(rank='4', suit='spades'),card(rank='3', suit='spades'),card(rank='7', suit='hearts'),card(rank='9', suit='clubs'),card(rank='j', suit='clubs'),card(rank='j', suit='diamonds'),card(rank='10', suit='spades'),card(rank='4', suit='diamonds'),card(rank='7', suit='spades'),card(rank='9', suit='diamonds'),card(rank='2', suit='hearts'),card(rank='6', suit='clubs'),card(rank='k', suit='clubs'),card(rank='4', suit='clubs'),card(rank='a', suit='hearts'),card(rank='k', suit='spades'),card(rank='2', suit='clubs'),card(rank='j', suit='spades'),card(rank='8', suit='spades'),card(rank='5', suit='spades'),card(rank='9', suit='spades'),card(rank='10', suit='clubs'),card(rank='8', suit='clubs'),card(rank='6', suit='diamonds'),card(rank='5', suit='clubs')
|
或者 在类外打补丁 frenchdeck.__setitem__ = 函数
1
2
3
4
|
def set_card(deck, position, card):
deck._cards[position] = card
frenchdeck.__setitem__ = set_card
|
3. 抽象基类
1
2
3
4
5
6
|
class test:
def __len__( self ):
return 24
from collections import abc
print ( isinstance (test(), abc.sized)) # true
|
只要实现了 __len__() 方法,就可以被 abc.sized 识别为子类
4. 不要直接子类化内置类型
1
2
3
4
5
6
7
8
9
10
11
|
class answerdict( dict ):
def __getitem__( self , key):
return 24
ad = answerdict(a = "good" )
print (ad[ 'a' ]) # 24
d = {}
d.update(ad)
print (d[ 'a' ]) # good
print (ad) # {'a': 'good'}
print (d) # {'a': 'good'}
|
直接子类化内置类型(如 dict、list 或 str)容易出错, 因为 内置类型的方法 通常会 忽略用户覆盖的方法。
不要子类化内置 类型,用户自己定义的类 应该继承 collections 模块 中的类,例如 userdict、userlist 和 userstring,这些类做了特殊设计,因 此易于扩展
5. 继承顺序
1
2
3
4
5
6
7
8
9
10
11
12
|
import collections
class answerdict2(collections.userdict):
def __getitem__( self , key):
return 24
ad = answerdict2(a = "good" )
print (ad[ 'a' ]) # 24
d = {}
d.update(ad)
print (d[ 'a' ]) # 24
print (ad) # {'a': 'good'}
print (d) # {'a': 24}
|
- 多重继承的同名方法调用,根据类的 __mro__ 属性顺次在类中查找
- 推荐使用 super() 函数
6. 重载运算符
不可变类型,,一定不能实现就地特殊方法
__iadd__等增量赋值特殊方法 必须返回 self
总结
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注服务器之家的更多内容!
原文链接:https://blog.csdn.net/qq_21201267/article/details/119203009