比方说我有一个
class Rectangle(object):
def __init__(self, length, width, height=0):
self.l = length
self.w = width
self.h = height
if not self.h:
self.a = self.l * self.w
else:
from itertools import combinations
args = [self.l, self.w, self.h]
self.a = sum(x*y for x,y in combinations(args, 2)) * 2 #thanks SO
#original code:
#(self.l * self.w * 2) + \
#(self.l * self.h * 2) + \
#(self.w * self.h * 2)
self.v = self.l * self.w * self.h
第12行的每个人都有什么看法?
self.a = sum(x*y for x,y in combinations(args, 2)) * 2
我听说应该避免显式列表索引引用.
是否有一个我可以使用的函数,如sum(),但仅用于乘法?
感谢大家的帮助.