一.类的定义 在Python中,一切皆对象,即便是类本身,也是一种type类型的特殊对象. class Person: def __init__(self, name, age): self.name = name self.age = age def sayHi(self): print ('Hello, my name is {}, my age is {}.'.format(self.name, self.age)) print(type(Person)) # 返回值为type 二.sel…
1.python类与对象各个算术运算魔法方法总结: 2.各个魔法方法应用举例: 3.实例训练: (1)我们都知道在 Python 中,两个字符串相加会自动拼接字符串,但遗憾的是两个字符串相减却抛出异常.因此,现在我们要求定义一个 Nstr 类,支持字符串的相减操作:A – B,从 A 中去除所有 B 的子字符串. class Nstr(str): def __sub__(self,other): self=list(self) other=list(other) for i i…