python类中的__init__和__new__的区别




class A:
    def __new__(cls, *args, **kwargs):
        """
        When a field is instantiated, we store the arguments that were used,
        so that we can present a helpful representation of the object.
        """
        instance = super(A, cls).__new__(cls)
        # instance._args = args
        # instance._kwargs = kwargs
        # print('__new__args:',args)
        # print("__new__kwargs:",kwargs)
        return instance
    def __init__(self,a,b):
        self.a=a
        self.b=b
        # print(self._args)
        # print(self._kwargs)
a=A(a=1,b=3)
print(a.a)
print(a.b)

你可能感兴趣的:(python)