比如内置类str,有个方法strip().我想修改strip()这个方法的参数和实现.那我该怎么做呢?如果不可以修改,那是否可以给内置类添加一个自定义的方法呢?如果可以,最好请给个简单的示例.多谢您的回复!
class laplace(str): def strip(self, chars=None, string='sorry'): return 'something %s' % string x = laplace() print x.strip()
Is this the effect you want by inheriting str and overriding the parent class method?
Python can directly overload, that is to say, you can implement a new strip function on the subclass, so that it is covered.
Is this the effect you want by inheriting str and overriding the parent class method?
Python can directly overload, that is to say, you can implement a new strip function on the subclass, so that it is covered.