python怎么修改内置类?
巴扎黑
巴扎黑 2017-04-18 09:43:51
0
2
510

比如内置类str,有个方法strip().
我想修改strip()这个方法的参数和实现.
那我该怎么做呢?
如果不可以修改,那是否可以给内置类添加一个自定义的方法呢?
如果可以,最好请给个简单的示例.
多谢您的回复!

巴扎黑
巴扎黑

reply all(2)
巴扎黑
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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!