Python中获取对象属性有哪些方法汇总?
- 内容介绍
- 文章标签
- 相关推荐
本文共计2838个文字,预计阅读时间需要12分钟。
本文将简单介绍四种获取对象的方法。
假设有以下类:
pythonclass Person(object): def __init__(self, name, age): self.name=name self.age=age
def __str__(self): return 'name=%s, age=%s' % (self.name, self.age)
方法一:直接通过实例变量访问
pythonperson=Person('Alice', 30)print(person.name) # 输出: Aliceprint(person.age) # 输出: 30
本文将简单介绍四种获取对象的方法。
本文共计2838个文字,预计阅读时间需要12分钟。
本文将简单介绍四种获取对象的方法。
假设有以下类:
pythonclass Person(object): def __init__(self, name, age): self.name=name self.age=age
def __str__(self): return 'name=%s, age=%s' % (self.name, self.age)
方法一:直接通过实例变量访问
pythonperson=Person('Alice', 30)print(person.name) # 输出: Aliceprint(person.age) # 输出: 30
本文将简单介绍四种获取对象的方法。

