函数是Python中的一流对象,可以分配给类或实例的属性。与Wikipedia示例相同的方法之一是:
colours = {"black": "000","red": "f00","green": "0f0","yellow": "ff0","blue": "00f","magenta": "f0f","cyan": "0ff","white": "fff"}class MyString(str): passfor name, pre in colours.iteritems(): def _in_colour(self, pre=pre): return '<span >%s</span>' % (pre, self) setattr(MyString, "in_" + name, _in_colour)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)