这里我定义了不可变类 str。在 新方法中,我将“hello”等实例的值更改为大写。当我们可以在init中定义 upper 时,为什么要使用new来实现呢?
class Upperstr(str):
def __new__(cls,value=""):
print(cls)
print(value)
return str.__new__(cls,value.upper())
# def __init__(self,m1):
# self.m1 = m1.upper()
u = Upperstr("hello")
print(u)
Run Code Online (Sandbox Code Playgroud)
New 用于创建类实例。新方法还有哪些其他用途?