python 对象的常见方法

class User:
def __init__(self, name, age, address, course, grade=None):
self.name = name
self.__age = age
self.address = address
self.course = course
self.grade = grade

def test(self):
print("test")


u = User("1", 2, 3, 4)
print(u.name,u.address,u._User__age)
print(u) # 对象类型 <__main__.User object at 0x1068aa2b0>
print(u.__dict__) # 把实例对象中的属性转为json
print(type(u.__dict__)) # 返回<class 'dict'>

print(u.__getattribute__("name")) # 获取属性的值
u.__delattr__("name") # 删除对象的属性

print(u.__getattribute__("__age")) # 返回 AttributeError: 'User' object has no attribute '__age'
print(u.__getattribute__("age")) # AttributeError: 'User' object has no attribute 'age'
print(u.__getattribute__("_User__age")) # 获取隐藏属性的值
posted @ 2021-03-11 09:33  Mia妈妈加油呀  阅读(95)  评论(0)    收藏  举报