python運算子過載程式設計 ne 怎麼用

2021-10-13 07:02:23 字數 4778 閱讀 1172

在python語言中提供了類似於c++的運算子重在功能:

一下為python運算子重在呼叫的方法如下:

method         overloads         call for

__init__        建構函式         x=class()

__del__         析構函式         物件銷毀

__add__         +                 x+y,x+=y

__or__         |                 x|y,x|=y

__repr__        列印轉換         print x,repr(x)

__str__         列印轉換         print x,str(x)

__call__        呼叫函式         x()

__getattr_    限制             x.undefine

__setattr__     取值             x.any=value

__getitem__     索引             x[key],

__len__         長度             len(x)

__cmp__         比較             x==y,x

__lt__         小於             x

__eq__         等於             x=y

__radd__        right-side +         +x

__iadd__        +=                 x+=y

__iter__        迭代             for in

1. 減法過載

class number:

def __init__(self, start):

self.data = start

def __sub__(self, other): #minus method

return number(self.data - other)

number = number(20)

y = number – 10 # invoke __sub__ method

class number:

def __init__(self, start):

self.data = start

def __sub__(self, other): #minus method

return number(self.data - other)

number = number(20)

y = number – 10 # invoke __sub__ method

2. 迭代過載

class indexer:

def __getitem__(self, index): #iter override

return index ** 2

x = indexer()

x[2]

for i in range(5):

print x[i]

class indexer:

def __getitem__(self, index): #iter override

return index ** 2

x = indexer()

x[2]

for i in range(5):

print x[i]

3. 索引過載

class stepper:

def __getitem__(self, i):

return self.data[i]

x = stepper()

x.data = 'spam'

x[1] #call __getitem__

for item in x: #call __getitem__

print item

class stepper:

def __getitem__(self, i):

return self.data[i]

x = stepper()

x.data = 'spam'

x[1] #call __getitem__

for item in x: #call __getitem__

print item

4. getattr/setattr過載

class empty:

def __getattr__(self,attrname):

if attrname == 'age':

return 40

else:

raise attributeerror,attrname

x = empty()

print x.age #call__getattr__

class accesscontrol:

def __setattr__(self, attr, value):

if attr == 'age':

# self.attrname = value loops!

self.__dict__[attr] = value

else:

print attr

raise attributeerror, attr + 'not allowed'

x = accesscontrol()

x.age = 40 #call __setattr__

x.name = 'wang' #raise exception

class empty:

def __getattr__(self,attrname):

if attrname == 'age':

return 40

else:

raise attributeerror,attrname

x = empty()

print x.age #call__getattr__

class accesscontrol:

def __setattr__(self, attr, value):

if attr == 'age':

# self.attrname = value loops!

self.__dict__[attr] = value

else:

print attr

raise attributeerror, attr + 'not allowed'

x = accesscontrol()

x.age = 40 #call __setattr__

x.name = 'wang' #raise exception

5. 列印過載

class adder:

def __init__(self, value=0):

self.data = value

def __add__(self, other):

self.data += other

class addrepr(adder):

def __repr__(self):

return 'addrepr(%s)' % self.data

x = addrepr(2) #run __init__

x + 1 #run __add__

print x #run __repr__

class adder:

def __init__(self, value=0):

self.data = value

def __add__(self, other):

self.data += other

class addrepr(adder):

def __repr__(self):

return 'addrepr(%s)' % self.data

x = addrepr(2) #run __init__

x + 1 #run __add__

print x #run __repr__

6. call呼叫函式過載

class prod:

def __init__(self, value):

self.value = value

def __call__(self, other):

return self.value * other

p = prod(2) #call __init__

print p(1) #call __call__

print p(2)

class prod:

def __init__(self, value):

self.value = value

def __call__(self, other):

return self.value * other

p = prod(2) #call __init__

print p(1) #call __call__

print p(2)

7. 析構函式過載

class life:

def __init__(self, name='name'):

print 'hello', name

self.name = name

def __del__(self):

print 'goodby', self.name

brain = life('brain') #call __init__

brain = 'loretta' # call __del__

python運算子過載程式設計 ne 怎麼用

這篇文章主要介紹了python運算子過載詳解及例項 的相關資料,需要的朋友可以參考下 python運算子過載 python語言提供了運算子過載功能,增強了語言的靈活性,這一點與c 有點類似又有些不同。鑑於它的特殊性,今天就來討論一下python運算子過載。python語言本身提供了很多魔法方法,它的...

Python 運算子過載

在 python 中is 是兩個運算子,對物件進行比較,分別對id,type value 進行比較。is 比較id type value三維 而 僅 比較value。實驗發現其實is,僅僅是比較一些簡單的基礎變數。class test object def init self self.value ...

Python運算子過載

print 呼叫父類建構函式 def parentmethod self print 呼叫父類方法 def setattr self,attr self.parentattr attr def getattr self print 父類屬性 self.parentattr def del self ...