临摹微笑
example (Buffer support << , Fee support + / +=)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647#!/usr/bin/env python# coding: utf-8 class Buffer(object): def __init__(self): self.buffer = [] def __lshift__(self, data): self.buffer.append(data) return self def __str__(self): return repr(self.buffer) class Fee(object): def __init__(self, qty=0, amount=0): self.qty = qty self.amount = amount def __radd__(self, another): return self if not another else self + another def __add__(self, another): return Fee( qty=self.qty + another.qty, amount=self.amount + another.amount ) def __repr__(self): return "%s(%s)" % ( self.__class__.__name__, ', '.join([ '%s=%r' % item for item in self.__dict__.items() ]) ) def tester(): buff = Buffer() buff << Fee(1, 23.5) << Fee(23, 1023.23) print sum(buff.buffer) if __name__ == "__main__": tester()