请教一下Python用类定义像replace一样的函数的方法

之前Python就见到很多函数,像replace这样的函数,“.”前面是字符串什么的,后面是函数,怎样定义这样的函数呢?有人说是用类,能不能给个示例?

python类如何定义使用更多参考:https://www.runoob.com/python/python-object.html
有帮助麻烦点个采纳【本回答右上角】,谢谢~~

class Test:
    def prt(self,str):
        print(str)
 
t = Test()
t.prt('hello word')

在类里面定义方法就可以了,类似这样

class a:
    def replaces(self, old, new):
        pass
    
aa = a().replaces('aa', 'bb')

如果对你有帮助,可以点击我这个回答右上方的【采纳】按钮,给我个采纳吗,谢谢

写一个类,里面定义一个方法,实现字符串replace功能,可这样写:

class mReplace:
    def mreplace(s,orig,repl):                
        for i in range(len(s)+len(repl)-len(orig)+1):
            if s[i:i+len(orig)] == orig:
                s = s[:i]+repl+s[i+len(orig):]
        return s
m = mReplace.mreplace('aabcdefaammdfdhaa','aa','okay')
print(m)

如对你有帮助,请点击我回答的右上方采纳按钮,采纳 一下。