类变量和对象变量的区别
对象变量为各对象自己所拥有,类变量所有对象所共有
class Circle: cnt = 0 # 类变量 # __init__初始化对象 def __init__(self, radius = 15): self.radius = radius Circle.cnt += 1 oneR = Circle() print('圆的半径:', oneR.radius) print('oneR.cnt: ', oneR.cnt) twoR = Circle(13) print('圆的半径:', twoR.radius) print('创建了{}个对象'.format(twoR.cnt)) print('Circle.__name__: ', Circle.__name__) print('Circle.__doc__: ', Circle.__doc__) print('Circle.__module__: ', Circle.__module__) print('oneR.cnt: ', oneR.cnt) print('twoR.cnt: ', twoR.cnt)
执行结果如下图所示: