1 >>> class CapStr(str): 2 def __new__(cls,string): 3 string = string.upper() 4 return str.__new__(cls,string) 5 6 7 >>> a = CapStr('afsgdfqwehu7348') 8 >>> a 9 'AFSGDFQWEHU7348'
__new__方法首先调用并返回一个实例化对象
1 >>> class CapStr(str): 2 def __new__(cls,string): 3 string = string.upper() 4 return str.__new__(cls,string) 5 6 7 >>> a = CapStr('afsgdfqwehu7348') 8 >>> a 9 'AFSGDFQWEHU7348'
__new__方法首先调用并返回一个实例化对象