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

 

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)         这是初始化实例

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-03
  • 2022-01-16
  • 2022-12-23
  • 2022-02-19
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-08
  • 2022-12-23
  • 2022-12-23
  • 2021-11-21
  • 2022-12-23
  • 2021-12-11
  • 2022-12-23
相关资源
相似解决方案