一、方法
1.方法
1)对象方法
- 怎么定义:直接定义在类中的函数
- 怎么调用:通过对象调用
- 特点:有默认参数self,self在调用时不传参
- 什么时候用:如果实现函数的功能需要对象属性就使用对象方法(对象方法中的self可以用来提供需要所有的对象属性)
2)类方法
- 怎么定义:在类中定义函数前加@classmethod
- 怎么调用:通过类调用
- 特点:自带参数cls,cls在调用时也不需要传参,系统会自动将当前类传给cls(谁调用就指向谁)
- 什么时候用:实现函数功能在不需要对象属性的时候就需要类,就用类方法
3)静态方法
- 怎么定义:在类中定义函数前加装饰器@staticmethod
- 怎么调用:通过类调用
- 特点:没有默认参数
- 什么时候用:实现函数功能的时候不需要类也不需要对象属性就用静态方法
class A:
# 对象方法
def func1(self):
print('对象方法', self)
# 类方法
@classmethod
def func2(cls):
print('类方法')
# 静态方法
@staticmethod
def func3():
print('静态方法')
2.方法在定义的时候怎么确定参数
- 看实现函数的功能除了类中的属性以外需不需要额外的数据,需要几个
3.方法的调用问题
1)本质上,不管什么方法都可以用对象和类调用
class A:
# 对象方法
def func1(self):
print('对象方法', self)
A.func1(100) # 注意:类可以调用对象方法,但是self就必须自己传参。失去可以指向当前对象的意义
# 注意:对象可以调用类方法,调用的时候cls还是指向当前类,但是会消耗额外的资源
二、对象属性的增删改查
class Person:
def __init__(self, name, tell, age=18, gender='男'):
self.name = name
self.age = age
self.tell = tell
self.gender = gender
# __ repr __会在当前类的对象被打印的时候自动调用,并且打印结果就是这个函数的返回值。(返回值必须是字符串)
def __repr__(self):
return str(self.__dict__)
p1 = Person('小明', '110')
p2 = Person('小花', '120', 20, '女')
print(p1) # {'name': '小明', 'age': 18, 'tell': '110', 'gender': '男'}
1.查(获取对象属性的值)
- a. 对象.属性
- b. getattr(对象,属性名)/(对象,属性名,默认值)
value = 'name'
print(p1.name) # 小明
print(getattr(p1, value, 0)) # 小明
2.增、改
- a. 对象.属性 = 值
- b. setattr(对象,属性名,值)
p1.age = 20
print(p1) # {'name': '小明', 'age': 20, 'tell': '110', 'gender': '男'}
p1.num = 123
print(p1) # {'name': '小明', 'age': 20, 'tell': '110', 'gender': '男', 'num': 123}
setattr(p1, 'age', 10)
print(p1) # {'name': '小明', 'age': 10, 'tell': '110', 'gender': '男', 'num': 123}
setattr(p1, 'hei', 175)
print(p1) # {'name': '小明', 'age': 10, 'tell': '110', 'gender': '男', 'num': 123, 'hei': 175}
3.删
- a. del 对象.属性
- b. delattr(对象,属性名)
del p1.gender
print(p1) # {'name': '小明', 'age': 10, 'tell': '110', 'num': 123, 'hei': 175}
delattr(p1, 'hei')
print(p1) # {'name': '小明', 'age': 10, 'tell': '110', 'num': 123}
三、内置类的属性
1. __ doc __ - 获取类的说明文档
class Dog:
'''狗类'''
num = 20
def __init__(self,name,age=18,color='白色',gender='公'):
self.name = name
self.gender = gender
self.age = age
self.color = color
def show_message(self):
print(self.name,self.gender,self.age,self.color)
@classmethod
def show_num(cls):
print('狗的数量',cls.num)
@staticmethod
def info():
print('狗是人类的朋友')
dog = Dog('柴柴')
# 1.__ doc __ - 获取类的说明文档
print(Dog.__doc__) # 狗类
2.__ class __ - 获取对象的类型(对象属性),功能和type()函数一样
print(dog.__class__) # <class '__main__.Dog'>
3.__ name __ - 获取类名(类属性)
print(Dog.__name__) # Dog
4.__ module __ - 获取所在的模块的模块名(类属性)
print(Dog.__module__) # __main__ 指在当前模块
5.__ dict __ - 获取指定类所有的类属性及其对应的值(以字典形式返回)
- 获取指定对象所有的对象属性及其对应的值
print(Dog.__dict__)
print(dog.__dict__) # {'name': '柴柴', 'gender': '公', 'age': 18, 'color': '白色'}
6.__ base __ - 获取指定类的父类
__ bases __ - 获取指定类的父类们
print(Dog.__base__) # <class 'object'>
四、运算符重载
1.python中的运算符
-
python中每一个运算符都对应一个固定的魔法方法,每次使用运算符的时候本质就是去调用对应方法。
-
某种类型的数据是否支持某种运算和这个类型中有没有实现运算符对应的魔法方法有关
2.在自己的类中重载指定运算符
class Student:
def __init__(self, name, age=18, score=0):
self.name = name
self.age = age
self.score = score
# 小于运算魔法方法
def __lt__(self, other):
return self.age < other.age
def __repr__(self):
return f'<{str(self.__dict__)[1:-1]}>'
st1 = Student('小明',score=90)
st2 = Student('小红',age=20,score=80)
st3 = Student('tom',age=31,score=60)
print(st1 != st2) # True 默认支持相等于运算
students = [st1,st2,st3]
students.sort()
print(students) # [<'name': '小明', 'age': 18, 'score': 90>, <'name': '小红', 'age': 20, 'score': 80>, <'name': 'tom', 'age': 31, 'score': 60>]