多态设计 zen of python poem 显式而非隐式 延迟赋值
总结
1、python支持延迟赋值,但是给调用者带来了困惑;
2、显式而非隐式,应当显式地指定要初始化的变量
class Card:
def __init__(self, rank, suit):
self.suit = suit
self.rank = rank
self.hard, self.soft = suit._points()
class NumberCard(Card):
def _points(self):
return int(self.rank), int(self.rank)
class AceCard(Card):
def _points(self):
return 1, 11
class FaceCard(Card):
def _points(self):
return 10, 10
class Rectangle:
def area(self):
return self.length * self.width
r = Rectangle()
r.length, r.width = 13, 8
r.area()