一 继承
继承是一种什么是什么的关系。
class People: pass class Animal: pass class Student(People,Animal): pass print(Student.__bases__)print(People.__bases__)
输出:
(<class ‘__main__.People‘>, <class ‘__main__.Animal‘>)(<class ‘object‘>,)
People,Animal成为Student的父类或基类。查看父类的方法是__bases__.。没有父类是默认继承Object类。
但凡继承了Object的类,称为新式类。
没有继承Object的类,称为经典类。
在python3中都是新式类。
类的继承,对象的名称空间
class People: def __init__(self,name,age): self.name=name self.age=age def walk(self): print(‘%s can walk‘%self.name) class Student(People): pass s=Student(‘egon‘,25) print(s.__dict__) print(Student.__dict__) print(People.__dict__)
输出:
{‘name‘: ‘egon‘, ‘age‘: 25} {‘__module__‘: ‘__main__‘, ‘__doc__‘: None} {‘__module__‘: ‘__main__‘, ‘__init__‘: <function People.__init__ at 0x000001E6B259ABF8>, ‘walk‘: <function People.walk at 0x000001E6B259AF28>, ‘__dict__‘: <attribute ‘__dict__‘ of ‘People‘ objects>, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘People‘ objects>, ‘__doc__‘: None}
结论,对象的名称空间又__init__方法初始化决定。不管__
时间: 2024-12-28 01:31:23