博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【转】Python3 操作符重载方法
阅读量:6997 次
发布时间:2019-06-27

本文共 14433 字,大约阅读时间需要 48 分钟。

Python3 操作符重载方法


本文由 Luzhuo 编写,转发请保留该信息.

原文: http://blog.csdn.net/Rozol/article/details/70769628


以下代码以Python3.6.1为例

Less is more!
操作符重载方法: 类(class)通过使用特殊名称的方法(len(self))来实现被特殊语法(len())的调用

#coding=utf-8# specialfuns.py 操作符重载方法# 类(class)通过使用特殊名称的方法(__len__(self))来实现被特殊语法(len())的调用# 构造 与 析构 方法class demo1:    # 构造方法, 对象实例化时调用    def __init__(self):        print("构造方法")    # 析构方法, 对象被回收时调用    def __del__(self):        print("析构方法")# newclass demo2(object):    # __init__之前调用, 一般用于重写父类的__new__方法, 具体使用见 类 文章的 元类 代码部分(http://blog.csdn.net/rozol/article/details/69317339)    def __new__(cls):        print("new")        return object.__new__(cls)# 算术运算class demo3:    def __init__(self, num):        self.data = num    # +    def __add__(self, other):        return self.data + other.data    # -    def __sub__(self, other):        return self.data - other.data    # *    def __mul__(self, other):        return self.data * other.data    # /    def __truediv__(self, other):        return self.data / other.data    # //    def __floordiv__(self, other):        return self.data // other.data    # %    def __mod__(self, other):        return self.data % other.data    # divmod()    def __divmod__(self, other):        # 商(10/5),余数(10%5)        return self.data / other.data, self.data % other.data    # **    def __pow__(self, other):        return self.data ** other.data    # <<    def __lshift__(self, other):        return self.data << other.data    # >>    def __rshift__(self, other):        return self.data >> other.data    # &    def __and__(self, other):        return self.data & other.data    # ^    def __xor__(self, other):        return self.data ^ other.data    # |    def __or__(self, other):        return self.data | other.dataclass none:    def __init__(self, num):        self.data = num# 反算术运算符(a+b, 若a不支持算术运算符,则寻找b的算术运算符)(注:位置变换, 在原始函数名前+r)class demo4:    def __init__(self, num):        self.data = num    # +    def __radd__(self, other):        return other.data + self.data    # -    def __rsub__(self, other):        return other.data - self.data    # *    def __rmul__(self, other):        return other.data * self.data    # /    def __rtruediv__(self, other):        return other.data / self.data    # //    def __rfloordiv__(self, other):        return other.data // self.data    # %    def __rmod__(self, other):        return other.data % self.data    # divmod()    def __rdivmod__(self, other):        return other.data / self.data, other.data % self.data    # **    def __rpow__(self, other):        return other.data ** self.data    # <<    def __rlshift__(self, other):        return other.data << self.data    # >>    def __rrshift__(self, other):        return other.data >> self.data    # &    def __rand__(self, other):        return other.data & self.data    # ^    def __rxor__(self, other):        return other.data ^ self.data    # |    def __ror__(self, other):        return other.data | self.data# 增量赋值运算,(注:位置同原始函数,在原始函数名前+i)class demo5():    def __init__(self, num):        self.data = num    # +=    def __iadd__(self, other):        return self.data + other    # -=    def __isub__(self, other):        return self.data - other    # *=    def __imul__(self, other):        return self.data * other    # /=    def __itruediv__(self, other):        return self.data / other    # //=    def __ifloordiv__(self, other):        return self.data // other    # %=    def __imod__(self, other):        return self.data % other    # **=    def __ipow__(self, other):        return self.data ** other    # <<=    def __ilshift__(self, other):        return self.data << other    # >>=    def __irshift__(self, other):        return self.data >> other    # &=    def __iand__(self, other):        return self.data & other    # ^=    def __ixor__(self, other):        return self.data ^ other    # |=    def __ior__(self, other):        return self.data | other# 比较运算符class demo6:    def __init__(self, num):        self.data = num    # <    def __lt__(self, other):        return self.data < other.data    # <=    def __le__(self, other):        return self.data <= other.data    # ==    def __eq__(self, other):        return self.data == other.data    # !=    def __ne__(self, other):        return self.data != other.data    # >    def __gt__(self, other):        return self.data > other.data    # >=    def __ge__(self, other):        return self.data >= other.data# 一元操作符class demo7:    def __init__(self, num):        self.data = num    # + 正号    def __pos__(self):        return +(self.data)    # - 负号    def __neg__(self):        return -abs(self.data)    # abs() 绝对值    def __abs__(self):        return abs(self.data)    # ~ 按位取反    def __invert__(self):        return ~self.data    # complex() 字符转数字    def __complex__(self):        return 1+2j    # int() 转为整数    def __int__(self):        return 123    # float() 转为浮点数    def __float__(self):        return 1.23    # round()  近似值    def __round__(self):        return 1.123# 格式化class demo8:    # print() 打印    def __str__(self):        return "This is the demo."    # repr() 对象字符串表示    def __repr__(self):        return "This is a demo."    # bytes() 对象字节字符串表现形式    def __bytes__(self):        return b"This is one demo."    # format() 格式化    def __format__(self, format_spec):        return self.__str__()# 属性访问class demo9:    # 获取(不存在)属性    def __getattr__(self):        print ("访问的属性不存在")    # getattr() hasattr() 获取属性    def __getattribute__(self, attr):        print ("访问的属性是%s"%attr)        return attr    # setattr() 设置属性    def __setattr__(self, attr, value):        print ("设置 %s 属性值为 %s"%(attr, value))    # delattr() 删除属性    def __delattr__(self, attr):        print ("删除 %s 属性"%attr)# ===================================================================# 描述器(类(test1)的实例出现在属主类(runtest)中,这些方法才会调用)(注:函数调用,这些方法不会被调用)class test1:    def __init__(self, value = 1):        self.value = value * 2    def __set__(self, instance, value):        print("set %s %s %s"%(self, instance, value))        self.value = value * 2    def __get__(self, instance, owner):        print("get %s %s %s"%(self, instance, owner))        return self.value    def __delete__(self, instance):        print("delete %s %s"%(self, instance))        del self.valueclass test2:    def __init__(self, value = 1):        self.value = value + 0.3    def __set__(self, instance, value):        print("set %s %s %s"%(self, instance, value))        instance.t1 = value + 0.3    def __get__(self, instance, owner):        print("get %s %s %s"%(self, instance, owner))        return instance.t1    def __delete__(self, instance):        print("delete %s %s"%(self, instance))        del self.valueclass runtest:    t1 = test1()    t2 = test2()# ---# 自定义propertyclass property_my:    def __init__(self, fget=None, fset=None, fdel=None):        self.fget = fget        self.fset = fset        self.fdel = fdel    # 对象被获取(self自身, instance调用该对象的对象(demo9), owner调用该对象的对象类对象(demo9))    def __get__(self, instance, owner):        print("get %s %s %s"%(self, instance, owner))        return self.fget(instance)    # 对象被设置属性时    def __set__(self, instance, value):        print("set %s %s %s"%(self, instance, value))        self.fset(instance, value)    # 对象被删除时    def __delete__(self, instance):        print("delete %s %s"%(self, instance))        self.fdel(instance)class demo10:    def __init__(self):        self.num = None    def setvalue(self, value):        self.num = value    def getvalue(self):        return self.num    def delete(self):        del self.num    x = property_my(getvalue, setvalue, delete)# ===================================================================# 自定义容器class lis:    def __init__(self, *args):        self.lists = args        self.size = len(args)        self.startindex = 0        self.endindex = self.size    # len() 容器元素数量    def __len__(self):        return self.size;    # lis[1] 获取元素    def __getitem__(self, key = 0):        return self.lists[key]    # lis[1] = value 设置元素    def __setitem__(self, key, value):        pass    # del lis[1] 删除元素    def __delitem__(self, key):        pass    # 返回迭代器    def ____(self):        return self    # rversed() 反向迭代器    def __reversed__(self):        while self.endindex > 0:            self.endindex -= 1            yield self[self.endindex]    # next() 迭代器下个元素    def __next__(self):        if self.startindex >= self.size:            raise StopIteration # 控制迭代器结束        elem = self.lists[self.startindex]        self.startindex += 1        return elem    # in / not in    def __contains__(self, item):        for i in self.lists:            if i == item:                return True        return False# yield 生成器(执行一次返回,下次继续执行后续代码返回)def yielddemo():    num = 0    while 1: # 1 == True; 0 == False        if num >= 10:            raise StopIteration        num += 1        yield num# 能接收数据的生成器def yielddemo_1():    while 1:        num = yield        print(num)# with 自动上下文管理class withdemo:    def __init__(self, value):        self.value = value    # 返回值为 as 之后的值    def __enter__(self):        return self.value    # 执行完成,退出时的数据清理动作    def __exit__(self, exc_type, exc_value, traceback):        del self.valueif __name__ == "__main__":    # 构造与析构    d1 = demo1()    del d1    # new    d2 = demo2()    # 算术运算符    d3 = demo3(3)    d3_1 = demo3(5)    print(d3 + d3_1)    print(d3 - d3_1)    print(d3 * d3_1)    print(d3 / d3_1)    print(d3 // d3_1)    print(d3 % d3_1)    print(divmod(d3, d3_1))    print(d3 ** d3_1)    print(d3 << d3_1)    print(d3 >> d3_1)    print(d3 & d3_1)    print(d3 ^ d3_1)    print(d3 | d3_1)    # 反运算符    d4 = none(3)    d4_1 = demo4(5)    print(d4 + d4_1)    print(d4 - d4_1)    print(d4 * d4_1)    print(d4 / d4_1)    print(d4 // d4_1)    print(d4 % d4_1)    print(divmod(d4, d4_1))    print(d4 ** d4_1)    print(d4 << d4_1)    print(d4 >> d4_1)    print(d4 & d4_1)    print(d4 ^ d4_1)    print(d4 | d4_1)    # 增量赋值运算(测试时注释其他代码)     = demo5(3)    d5 <<= 5    d5 >>= 5    d5 &= 5    d5 ^= 5    d5 |= 5    d5 += 5    d5 -= 5    d5 *= 5    d5 /= 5    d5 //= 5    d5 %= 5    d5 **= 5    print(d5)    # 比较运算符    d6 = demo6(3)    d6_1 = demo6(5)    print(d6 < d6_1)    print(d6 <= d6_1)    print(d6 == d6_1)    print(d6 != d6_1)    print(d6 > d6_1)    print(d6 >= d6_1)    # 一元操作符(测试时注释其他代码)    d7 = demo7(-5)    num = +d7    num = -d7    num = abs(d7)    num = ~d7    print(num)    print(complex(d7))    print(int(d7))    print(float(d7))    print(round(d7))    # 格式化    d8 = demo8()    print(d8)    print(repr(d8))    print(bytes(d8))    print(format(d8, ""))    # 属性访问    d9 = demo9()    setattr(d9, "a", 1) # => 设置 a 属性值为 1    print(getattr(d9, "a")) # => a / 访问的属性是a    print(hasattr(d9, "a")) # => True / 访问的属性是a    delattr(d9, "a") # 删除 a 属性    # ---    d9.x = 100 # => 设置 x 属性值为 100    print(d9.x) # => x / 访问的属性是x    del d9.x # => 删除 x 属性    # 描述器    r = runtest()    r.t1 = 100 # =>  <__main__.test1> <__main__.runtest> 100    print(r.t1) # => 200 /  <__main__.test1> <__main__.runtest> 
del r.t1 # => <__main__.test1> <__main__.runtest> r.t2 = 200 # => <__main__.test2> <__main__.runtest> 200 / <__main__.test1> <__main__.runtest> 200.3 print(r.t2) # => 400.6 / <__main__.test2> <__main__.runtest>
/ <__main__.test1> <__main__.runtest>
del r.t2 # <__main__.test2> <__main__.runtest> # --- # 自定义property d10 = demo10() d10.x = 100; # => <__main__.property_my> <__main__.demo10> 100 print(d10.x) # => 100 / <__main__.property_my> <__main__.demo10>
del d10.x # => <__main__.property_my> <__main__.demo10> d10.num = 200; print(d10.num) # => 200 del d10.num # 自定义容器(迭代器Iterator) lis = lis(1,2,3,4,5,6) print(len(lis)) print(lis[1]) print(next(lis)) print(next(lis)) print(next(lis)) for i in lis: print (i) for i in reversed(lis): print (i) print(3 in lis) print(7 in lis) print(3 not in lis) print(7 not in lis) # yield 生成器(可迭代对象Iterable) for i in yielddemo(): print (i) # --- iters = iter(yielddemo()) print(next(iters)) print(next(iters)) # --- 发送数据给生成器 --- iters = yielddemo_1() next(iters) iters.send(6) # 发送数据并执行 iters.send(10) # with 自动上下文管理 with withdemo("Less is more!") as s: print(s)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546

转载于:https://www.cnblogs.com/langqi250/p/10491861.html

你可能感兴趣的文章