鱼C论坛

 找回密码
 立即注册
查看: 3384|回复: 1

[已解决]python类和对象问题

[复制链接]
发表于 2022-12-26 11:41:43 | 显示全部楼层 |阅读模式
3鱼币
>>> class C:
...     x = []
...     def add_x(self, x):
...         self.x.append(x)
...
>>> c = C()
>>> d = C()
>>> c.add_x(250)
>>> d.add_x(520)
>>> c.x
请问这里为什么会打印[250,520]呢,不应该只打印[250]吗,用到的是列表的哪条属性呢?
最佳答案
2022-12-26 11:41:44
本帖最后由 lxping 于 2022-12-26 13:18 编辑

你定义的x属性为类属性,类名.属性名和类的实例化对象.属性名都可以访问和更改类属性x,
所以当使用d.add_x(520)后,类属性x就已经变成了[250,520],你使用类名C.x同样可以得到这个值[250,520]
要区分类属性和对象属性
类属性
  1. class C:
  2.      x = []
  3.      def add_x(self, x):
  4.          self.x.append(x)

  5.          
  6. c = C()
  7. d = C()
  8. c.add_x(520)
  9. d.add_x(250)
  10. c.x
  11. [520, 250]
  12. C.x
  13. [520, 250]
  14. d.x
  15. [520, 250]
  16. c.__dict__        #没有属性x
  17. {}
  18. d.__dict__        #没有属性x
  19. {}
  20. C.__dict__        #有属性x
  21. mappingproxy({'__module__': '__main__', 'x': [520, 250], 'add_x': <function C.add_x at 0x0000020C161CDCF0>, '__dict__': <attribute '__dict__' of 'C' objects>, '__weakref__': <attribute '__weakref__' of 'C' objects>, '__doc__': None})
复制代码

对象属性
  1. class A:
  2.      def __init__(self):
  3.          self.x = []
  4.      def add_x(self, x):
  5.          self.x.append(x)

  6.          
  7. a = A()
  8. b = A()
  9. a.add_x(520)
  10. b.add_x(250)
  11. a.x
  12. [520]
  13. b.x
  14. [250]
  15. a.__dict__        #有属性x
  16. {'x': [520]}
  17. b.__dict__        #有属性x
  18. {'x': [250]}
  19. A.__dict__        #没有属性x
  20. mappingproxy({'__module__': '__main__', '__init__': <function A.__init__ at 0x0000020C161CDEA0>, 'add_x': <function A.add_x at 0x0000020C161CDFC0>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None})
复制代码


最佳答案

查看完整内容

你定义的x属性为类属性,类名.属性名和类的实例化对象.属性名都可以访问和更改类属性x, 所以当使用d.add_x(520)后,类属性x就已经变成了[250,520],你使用类名C.x同样可以得到这个值[250,520] 要区分类属性和对象属性 类属性 对象属性
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-12-26 11:41:44 | 显示全部楼层    本楼为最佳答案   
本帖最后由 lxping 于 2022-12-26 13:18 编辑

你定义的x属性为类属性,类名.属性名和类的实例化对象.属性名都可以访问和更改类属性x,
所以当使用d.add_x(520)后,类属性x就已经变成了[250,520],你使用类名C.x同样可以得到这个值[250,520]
要区分类属性和对象属性
类属性
  1. class C:
  2.      x = []
  3.      def add_x(self, x):
  4.          self.x.append(x)

  5.          
  6. c = C()
  7. d = C()
  8. c.add_x(520)
  9. d.add_x(250)
  10. c.x
  11. [520, 250]
  12. C.x
  13. [520, 250]
  14. d.x
  15. [520, 250]
  16. c.__dict__        #没有属性x
  17. {}
  18. d.__dict__        #没有属性x
  19. {}
  20. C.__dict__        #有属性x
  21. mappingproxy({'__module__': '__main__', 'x': [520, 250], 'add_x': <function C.add_x at 0x0000020C161CDCF0>, '__dict__': <attribute '__dict__' of 'C' objects>, '__weakref__': <attribute '__weakref__' of 'C' objects>, '__doc__': None})
复制代码

对象属性
  1. class A:
  2.      def __init__(self):
  3.          self.x = []
  4.      def add_x(self, x):
  5.          self.x.append(x)

  6.          
  7. a = A()
  8. b = A()
  9. a.add_x(520)
  10. b.add_x(250)
  11. a.x
  12. [520]
  13. b.x
  14. [250]
  15. a.__dict__        #有属性x
  16. {'x': [520]}
  17. b.__dict__        #有属性x
  18. {'x': [250]}
  19. A.__dict__        #没有属性x
  20. mappingproxy({'__module__': '__main__', '__init__': <function A.__init__ at 0x0000020C161CDEA0>, 'add_x': <function A.add_x at 0x0000020C161CDFC0>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None})
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 06:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表