python-面向对象属性的访问与self的理解

Python91

属性访问

类属性与对象属性

在类中定义的名字,都是类的属性,细说的话,类有两种属性:数据属性和函数属性,可以通过 __dict__访问属性的值,比如 Person1.__dict__['student'],但Python提供了专门的属性访问语法

print(Person1.student )# 访问数据属性,等同于Person1.__dict__['student']
print(Person1.run)  # 访问函数属性,等同于Person1.__dict__['run']

"""结果如下"""

周杰伦

操作对象的属性也是一样

```python
print(per1.name) # print(per1.dict['name'])

输入验证码查看隐藏内容

扫描二维码关注本站微信公众号 Johngo学长
或者在微信里搜索 Johngo学长
回复 svip 获取验证码
wechat Johngo学长