python对象自省机制
自省是通过一定的机制查询到对象的内部结构;较为常减的函数用法有:dir(),type(),hasattr(),isinstance()。通过这些函数,我们能够在程序运行时得知对象的类型,判断对象是否存在某个属性,访问对象的属性。
class Person(object):
name
= "happy"
class Student(Person
):
def __init__(self
,h_name
):
self
.h_name
= h_name
g
= Student
("gd")
print(g
.__dict__
)
print(dir(g
))
super函数
class Person(object):
def __init__(self
, name
, age
, weight
):
self
.name
= name
self
.age
= age
self
.weight
= weight
def speak(self
):
print(f
"{self.name}说:{self.age}岁")
class Student(Person
):
def __init__(self
, name
, age
, weight
,grade
):
Person
.__init__
(self
, name
, age
, weight
)
self
.grade
= grade
def speak(self
):
print(f
"{self.name}说:{self.age}岁,我在{self.grade}年纪")
h
= Student
('happy', 24,89,7)
h
.speak
()