pythonpycharm 2009-07-12
代码如下:
class MyTest: myname = 'peter' def sayhello(hello): print "say hello to %s" % hello.myname if __name__ == "__main__": MyTest().sayhello()
代码如下:
class MyTest: myname = 'peter' def sayhello(self): print "say hello to %s" % self.myname if __name__ == "__main__": MyTest.myname = 'hone' MyTest.sayhello = lambda self,name: "I want say hello to %s" % name MyTest.saygoodbye = lambda self,name: "I do not want say goodbye to %s" % name print MyTest().sayhello(MyTest.myname) print MyTest().saygoodbye(MyTest.myname)
代码如下:
def enhanced(meth): def new(self, y): print "I am enhanced" return meth(self, y) return new class C: def bar(self, x): print "some method says:", x bar = enhanced(bar)
代码如下:
class C: @classmethod def foo(cls, y): print "classmethod", cls, y
class Singleton: def __new__: # 关键在于这,每一次实例化的时候,我们都只会返回这同一个instance对象 if not hasattr: cls.instance =