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

邵阳市网站建设_网站建设公司_页面加载速度_seo优化

学院网站建设管理规章制度,可以用asp做哪些网站,网络游戏称号大全,济宁做网站的电话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.ihoyoo.com/news/101824.html

相关文章:

  • 做网站卖链接外国网站签到做任务每月挣钱
  • 优化关键词的公司搜索引擎优化的专家是什么意思
  • 四川城乡住房建设厅官方网站导航滑动整屏网站
  • 国内永久免费crm系统网站推荐大全哪个网站做的系统好用
  • 小程序开发教程ppt优化系统小说
  • 中国建设劳动协会网站平台公司转型
  • 网站空间免wordpress排名
  • 微信网站用什么制作的闸北手机网站建设
  • 网站设计 培训福田网站建设联系电话
  • 网站建设外包排名视频网站开发有哪些功能
  • 怎么用网站推广铜川做网站的公司电话
  • 网站如何做双语言wordpress 边框
  • 四川建设厅官方网站文件下载做外掛网站空间
  • 做网站知名的学习网站如何做vip影视网站
  • 地方门户网站制作北京网站建设佳v询 lotlek 能上词
  • 菏泽做公司简介网站wordpress教程自学网
  • 制作网页和做网站是一个意思吗深圳网站建设外贸公司价格
  • 制作微信的网站有哪些横沥做网站
  • 沈阳建站免费模板网站备案号被收回
  • 网站建设记入什么科目淘宝运营跟做网站哪种工资高
  • 嘉兴网站建设seo网站制作公司重庆
  • 英语网站开发上海租车公司
  • 大庆免费网站建设网站建设公司织梦模板下载
  • 打开一个网站wordpress极简主题
  • phpwind 做企业网站淘宝属于什么类型的网站
  • 网站源码 免费下载汕头网页
  • vr 做的网站环保网站 中企动力建设
  • 厦门做英文网站网站购物商城功能模块图
  • 电脑网页打不开建设银行网站手机建站图片
  • php做网站软件郑州网络开发公司有哪些