当前位置: 首页 > news >正文

西宁网站建设官网WordPress理财模板

西宁网站建设官网,WordPress理财模板,北京网页设计模板,爱网站找不到了4.10.4 运算篇 Python的类型系统是鸭子类型#xff0c;也就是不检查某个具体的对象是什么类型#xff0c;而是检查这个对象有没有相应的功能。在Python中有大量的魔术方法#xff0c;我们可以通过魔术方法的方式为对象添加相应的功能。 下面介绍Python中和运算相关的魔术方…4.10.4 运算篇 Python的类型系统是鸭子类型也就是不检查某个具体的对象是什么类型而是检查这个对象有没有相应的功能。在Python中有大量的魔术方法我们可以通过魔术方法的方式为对象添加相应的功能。 下面介绍Python中和运算相关的魔术方法。这些魔术方法是对两个对象在进行符号运算时对相应的运算符号进行重载从而实现相应的效果。 在本篇所讲的运算相关的魔术方法中参数other并不一定要和self一致。 很多时候我们用魔术方法只是借用了这个符号并不一定要符号该符号原来的定义。比如我们可以将左移符号重载成C中的cout。 4.10.4.1 add addobj1 obj2 from icecream import icclass Vector:def __init__(self, x, y):self.x xself.y ydef __repr__(self):return fVertor({self.x}{self.y})def __add__(self, other):return Vector(self.x other.x, self.y other.y)v1 Vector(0, 1) v2 Vector(2, 3) print(v1 v2)Vertor(24) 4.10.4.2 sub subobj1 – obj2 4.10.4.3 mul、matmul、pow mulobj1 * obj2 matmulobj1 obj2 powobj1 ** obj2、pow(obj1 obj2 modNone)  (obj1 ** obj2) % mod 4.10.4.4 truediv、floordiv、mod、divmod truedivobj1 / obj2 floordivobj1 // obj2 modobj1 % obj2 divmoddivmod(obj1obj2) 4.10.4.5 lshift、rshift lshiftobj1 obj2 rshiftobj1 obj2 4.10.4.6 and、xor、or andobj1 obj2 xorobj1 ^ obj2 orobj1 | obj2 4.10.4.7 运算符的r版本 如下代码所示当我们自定义乘法判断相乘的对象是int类型时则将xy同时和int相乘并返回。但是如果是int类型数据和我们自定义的Vector类型相乘时则会报错因为是Python内置的int类型是不知道要如何和我们自定义的Vector类型相乘的。 from icecream import icclass Vector:def __init__(self, x, y):self.x xself.y ydef __repr__(self):return fVerctor({self.x}{self.y})def __mul__(self, other):# v1 * v2if isinstance(other, int):return Vector(self.x * other, self.y * other)v1 Vector(2, 3) ic(v1 * 2) # 本质v1.__mul__(2) ic(2 * v1)ic| v1 * 2: Verctor(46) Traceback (most recent call last): File “E:\BaiduSyncdisk\FrbPythonFiles\t2.py”, line 20, in ic(2 * v1) TypeError: unsupported operand type(s) for *: ‘int’ and ‘Vector’ 解决办法是定义一个__rmul__魔法方法如下 from icecream import icclass Vector:def __init__(self, x, y):self.x xself.y ydef __repr__(self):return fVerctor({self.x}{self.y})def __mul__(self, other):# v1 * v2if isinstance(other, int):return Vector(self.x * other, self.y * other)def __rmul__(self, other):# v1 * v2if isinstance(other, int):return Vector(self.x * other, self.y * other)v1 Vector(2, 3) ic(v1 * 2) ic(2 * v1)ic| v1 * 2: Verctor(46) ic| 2 * v1: Verctor(46) 这样一来如果操作符左侧的这个数据结构没有定义这个操作应该怎么完成的话Python就会尝试去找操作符右侧这个对象的操作符的r版本魔法方法。这样就可以正常完成int * Verctor的操作了。 这里之前列出的所有和数的计算有关的魔法方法都有其r版本。 4.10.4.8 运算符的i版本 i版本运算符魔法方法最后是修改self并返回。其实对应的符号就是操作符。比如v1 v2就会调用v1.iadd(v2)。 from icecream import icclass Vector:def __init__(self, x, y):self.x xself.y ydef __repr__(self):return fVertor({self.x}{self.y})def __add__(self, other):return Vector(self.x other.x, self.y other.y)def __iadd__(self, other):return Vector(self.x * other.x, self.y * other.y)v1 Vector(0, 1) v2 Vector(2, 3) ic(v1 v2) v1 v2 ic(v1)ic| v1 v2: Vertor(24) ic| v1: Vertor(03) 我们看到v v2 时调用了__iadd__魔术方法。返回结果Vertor(03)。但其实我们不创建这个__iadd__魔术方法也可以。 from icecream import icclass Vector:def __init__(self, x, y):self.x xself.y ydef __repr__(self):return fVertor({self.x}{self.y})def __add__(self, other):return Vector(self.x other.x, self.y other.y)v1 Vector(0, 1) v2 Vector(2, 3) ic(v1 v2) v1 v2 ic(v1)ic| v1 v2: Vertor(24) ic| v1: Vertor(24) 在本节前面提到的魔术方法中所有使用符号触发的都有其对应的i版本。也就是、-、*… 4.10.4.9 neg、pos 这2个魔术方法对应的是在数据结构之前添加一个-和符号。 neg-obj posobj from icecream import icclass Vector:def __init__(self, x, y):self.x xself.y ydef __repr__(self):return fVertor({self.x}{self.y})def __neg__(self):return Vector(-self.x, -self.y)def __pos__(self):return Vector(self.x, self.y)v1 Vector(-2, 3) ic(-v1) ic(v1)ic| -v1: Vertor(2-3) ic| v1: Vertor(-23) 4.10.4.10 abs、invert absabs(obj) invert~obj # 位运算中的反转 4.10.4.11 complex、int、float complexcomplexobj intintobj floatfloatobj 这3个魔术方法规定返回的结果必须是其对应的数据结构。 4.10.4.12 index index代表当你把这个数据结构当成index使用的时候等价于什么。 如果我们定义了这个__index__而没有定义__ complex__、int、__float__方法的话那么complex、int、float都会默认使用这个__index__魔法方法。 from icecream import icclass Vector:def __init__(self, x, y):self.x xself.y ydef __repr__(self):return fVertor({self.x}{self.y})def __index__(self):return int(self.x)v1 Vector(2, 3) lst list(Python) ic(lst[v1]) # [v1]返回2相当于lst[int(self.x)]ic| lst[v1]: ‘t’ 4.10.4.13 round、trunc、floor、ceil roundround(obj) # 四舍五入 truncmath.trunc(obj) # 无条件舍弃小数 floormath.floor(obj) # 向负无穷取整 ceilmath.ceil(obj) # 向正无穷取整
http://www.zqtcl.cn/news/808635/

相关文章:

  • 网站设计结果泸州市住房和城乡建设厅官方网站
  • php网站开发入门到精通教程旅游网站项目评估
  • 四川省城乡和住房建设厅网站首页通过邮箱查注册网站
  • 自己的网站怎么编辑如何做响应式的网站
  • 做一个产品网站要多少钱网站宣传与推广的方法
  • 郑州区块链数字钱包网站开发公司局部装修改造找哪家装修公司
  • 网站界面可以做版权吗学生网站建设的总结与评价
  • 用表格做网站10条重大新闻
  • 河南定制网站建设报价盐城网站建设制作工作室
  • 能看各种网站的浏览器wordpress文章名称背景
  • ppt做视频模板下载网站网站建设在哪些方面
  • 能用pinduoduo做网站吗网站建设行业的分析
  • 新乡商城网站建设价格做网站网站会怎么样
  • 给个网站能用的2022作品集模板
  • 做推送好用的网站科技资讯网站有哪些
  • 手机上如何制作网站wordpress阅读量随机生成
  • 汝州市住房和城乡建设局网站网站自己做流量
  • 怎么做网站有利于收录沭阳做网站公司排名前十
  • 给企业做网站如何定价电商系统平台
  • 山东川畅科技网站设计流量网站建设教程
  • 湖南省住房建设厅网站企业手机网站制作
  • 做虚假网站犯法吗发稿计划
  • 网站怎么防黑客网站建设一般要多大空间
  • 做网站一般要多钱织梦做网站被告
  • 响应式网站如何设计网页设计与制作课程标准化项目申报书
  • 苏州公司技术支持 苏州网站建设蔬菜类网站建设规划书
  • 主流做网站wordpress字体设置
  • 大连网站建设解决方案加快政务网站群建设管理
  • 淮南网站建设全包成都设计咨询集团有限公司
  • 网站管理助手v3wordpress主题防盗版