标签一下一个类实例的过程。

老年人怕忘记,还是写一笔记录下,以后查寻方便点。

class Car:
    def __new__(cls, *args, **kwargs):
        return object.__new__(cls)

    def __init__(self, color, mileage):
        self.color = color
        self.mileage = mileage

    def __repr__(self):
        return f'Car({self.color !r}, {self.mileage !r})'

    def __str__(self):
        return f'a {self.color} car'

 还是这个代码

执行car = Car('red', 8888)的时候

分为三步

第一:首先应该执行Car.__call__内部的函数 最后return 对象。

第二:在__call__内部首相应该执行car = object.__new__(Car)  创建实例

第三: car .__init__('red'.8888)         这是初始化实例

猜你喜欢

转载自www.cnblogs.com/sidianok/p/11923360.html