About 1 min
1. super()初始化
class Animal():
def __init__(self) -> None:
print('animal init')
class Cat(Animal):
pass
c = Cat()
# animal init
class Person():
def __init__(self, name) -> None:
print(f'name: {name}')
class Student(Person):
pass
p = Student('kitty')
# name: kitty
如果子类的初始化函数和父类的没什么区别,那么可以不用显式地写super().__init__()或者super().__init__(*args)
class Person:
def __init__(self, person_name, person_age):
self.name = person_name
self.age = person_age
class Student(Person):
def __init__(self, student_name, student_age, student_id):
super().__init__(student_name, student_age)
self.studentId = student_id
python 3 的 syntax 更简洁的等同替换:
- 父类:
class Person:class Person():
- 子类:
super().__init__(student_name, student_age):super()中的可以省略父类的名字和self参数,因为Python会自动推断它们Person.__init__(self, student_name, student_age): 这个self不能省略。- 注意:前者
__init__()中本来就没有self,后者__init__()本来就有。
python 2 的 syntax 等同替换(python3兼容python2的,反之不行):
- 父类:
class Person(object):,任何类都必须继承object - 子类:
super(Student, self).__init__(student_name, student_age),必须指明父类。
2. 静态方法
class Animal():
@staticmethod
def hello(): # 没有 self
print('hello')
Animal.hello() # Class
Animal().hello() # Object
3. 抽象类
可以使用抽象类,可以不实现抽象类的方法
class animal():
def speak(self):
pass
class cat(animal):
def say_hello(self):
print("hello")
a=animal()
c=cat()
c.say_hello()
c.speak()
不让实例化抽象类,
@abstractmethod的抽象方法必须实现

from abc import ABC,abstractmethod
class Animal(ABC):
def say_hello(self):
print("hello")
@abstractmethod
def speak(self):
pass
class Cat(Animal):
def speak(self):
print("speak")
pass
c = Cat()
c.speak()
c.say_hello()
类方法或静态方法, 则应分别使用 @abstractclassmethod 和 @abstractstaticmethod
另外,直接继承ABC也等同于指定元类metaclass=ABCMeta:
from abc import ABCMeta,abstractmethod
class Animal(metaclass=ABCMeta):
def say_hello(self):
print("hello")
@abstractmethod
def speak(self):
pass