摘要:
def ma(cls): print 'method a'def mb(cls): print 'method b'method_dict = { 'ma':ma, 'mb':mb}class DynamicMethod(type): def __new__(cls, name, bases, dct): if name[:3] == 'Abc': dct.update(method_dict) return type.__new__(cls, name, bases, dct) def __init__(c... 阅读全文
摘要:
1. 是否了解动态语言的鸭子模型?2. 是否了解可变参数与关键字参数?3. 对函数式编程有初步了解。4. 是否知道列表生成式?5. 是否知道lambda/decorator/slots?6. 为什么要把缺省参数设为immutable?7. 是否知道Mixin?8. 是否知道WSGI接口?9. 是否知道异步框架如gevent/tornado?10. 是否深入了解过Python的GC和GIL?11.是否了解python对象的查找机制? 阅读全文
摘要:
__dict__:实例化类时将实例属性分配到__dict____slots__:阻止实例分配属性到__dict__class Base(object): __slots__ = ['y', 'x'] def __init__(self): self.y = 'aa' self.x = 'xx'b = Base()print b.__dict__ #抛错print b.yb.x = 30print b.x 阅读全文
摘要:
1 class AttriubteTest(object): 2 def __init__(self): 3 self.a = 0.01 4 self.b = 1.22 5 6 def __getattribute__(self, item): 7 if item == 'a': 8 return self.a #抛RuntimeError: maximum recursion depth exceeded in cmp异常 9 else:10 return ob... 阅读全文
摘要:
_builtin_types = [ type, object, bool, complex, dict, float, int, list, slice, str, tuple, set, frozenset, Exception, type(None), types.BuiltinFunctionType, types.GeneratorType, types.MethodType, types.CodeType, types.FrameType, types.TracebackType, types.ModuleType, types.FunctionType... 阅读全文