gj3 Python数据模型(魔法函数)

3.1 什么是魔法函数

    类里面,实现某些特性的内置函数,类似 def __xx__(): 的形式。 不要自己定义XX,并不是和某个类挂钩的

class Company(object):
    def __init__(self, employee_list):
        self.employee = employee_list

    # 可迭代
    def __getitem__(self, item):
        return self.employee[item]

    # 长度,实现了len函数
    def __len__(self):
        return len(self.employee)


company1 = Company(["tom", "bob", "jane"])
#
# company1= company[:2]
#
# print(len(company))

for em in company1:
    print(em)

3.2 python的数据模型以及数据模型对python的影响

只要实现了对应的数据模型,就具有该模型的特性

3.3 魔法函数一览

     3.3.1 非数学运算

字符串表示

__repr__  开发模式下调用的

__str__   对对象进行字符串格式化时调用

class Company(object):
    def __init__(self, employee_list):
        self.employee=employee_list
    def __str__(self):
        print("str called")
        return",". join(self. employee)
    def __repr__(self):
        print("repr called")
        return",". join(self. employee)
company=Company(["tom","bob","jane"])
company
# repr(company)
# company.__repr__()
# print(company)

repr called
tom,bob,jane


    

Python数据模型(魔法函数)非数学运算
  字符串表示
    __repr__
    __str__
  集合、序列相关
    __len__
    __getitem__
    __setitem__
    __delitem__
    __contains__
  迭代相关
    __iter__
    __next__
  可调用
    __call__
  with上下文管理器
    __enter__
    __exit__
  数值转换
    __abs__
    __bool__
    __int__
    __float__
    __hash__
    __index__
  元类相关
    __new__
    __init__
  属性相关
    __getattr__、 __setattr__
    __getattribute__、setattribute__
    __dir__
  属性描述符
    __get__、__set__、 __delete__
  协程
    __await__、__aiter__、__anext__、__aenter__、__aexit__
Python数据模型(魔法函数)--非数学运算

3.3.2 数学运算

  一元运算符
    __neg__(-)、__pos__(+)、__abs__
  二元运算符
    __lt__(<)、 __le__ <= 、 __eq__ == 、 __ne__ != 、 __gt__ > 、 __ge__ >=
  算术运算符
    __add__ + 、 __sub__ - 、 __mul__ * 、 __truediv__ / 、 __floordiv__ // 、 __ mod__ % 、 __divmod__ divmod() 、 __pow__ ** 或 pow() 、 __round__ round()
  反向算术运算符
    __radd__ 、 __rsub__ 、 __rmul__ 、 __rtruediv__ 、 __rfloordiv__ 、 __rmod__ 、 __rdivmod__ 、 __rpow__
  增量赋值算术运算符
    __iadd__ 、 __isub__ 、 __imul__ 、 __itruediv__ 、 __ifloordiv__ 、 __imod__ 、 __ipow__
  位运算符
    __invert__ ~ 、 __lshift__ << 、 __rshift__ >> 、 __and__ & 、 __or__ | 、 __ xor__ ^
  反向位运算符
    __rlshift__ 、 __rrshift__ 、 __rand__ 、 __rxor__ 、 __ror__
  增量赋值位运算符
    __ilshift__ 、 __irshift__ 、 __iand__ 、 __ixor__ 、 __ior__
数学运算
class Nums(object):
    def __init__(self,num):
        self.num=num
    def __abs__(self):
        return abs(self.num)
my_num=Nums(1)
abs(my_num)
1class MyVector(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __add__(self, other_instance):
        re_vector = MyVector(self.x + other_instance.x, self.y + other_instance.y)
        return re_vector
    def __str__(self):
        return "x:{x},y:{y}".format(x=self.x, y=self.y)
​
​
first_vec = MyVector(1, 2)
second_vec = MyVector(2, 3)
print(first_vec + second_vec)
​
x:3,y:5

  

3.4 随便举个例子说明魔法函数的重要性(len函数)

len(set dict list) 会直接调用set dict list数据类型本身cpython的内置实现

在任何对象中都可以去实现魔法函数

只要实现了对应的魔法函数,就能实现Python某些数据类型的功能

猜你喜欢

转载自www.cnblogs.com/wenyule/p/10357951.html