Python 運算符,python中的運算符重載_Python中的操作符重載

 2023-11-18 阅读 29 评论 0

摘要:Python部落組織翻譯,禁止轉載,歡迎轉發。BY?ALOK THAKUR? FEBRUARY 22, 20162016年2月22日 , ALOK THAKUR寫Python支持許多內建的類型,例如列表,集合,字典等等。它還允許編程用戶來定義自己的類型通常被稱為用戶定義的類型。Python還有一些內置操作來操作所有對

Python部落組織翻譯,禁止轉載,歡迎轉發。

BY?ALOK THAKUR?· FEBRUARY 22, 2016

2016年2月22日 , ALOK THAKUR寫

Python支持許多內建的類型,例如列表,集合,字典等等。它還允許編程用戶來定義自己的類型通常被稱為用戶定義的類型。Python還有一些內置操作來操作所有對象的類型。似乎有點不合理,如果用戶沒有控制操作用戶定義的類型。

操作符重載來幫忙了。

Python 運算符?1. 操作符重載允許用戶定義類型攔截Python內置操作 。

2. 所有Python表達式運算符如“+”、“-”都可以重載

3. 內置的操作例如打印,屬性訪問等都可以重載。

4. Python在類中提供了專門命名方法來支持操作符重載

它允許用戶決定一個語言按照用戶定義的類型如何進行內置操作。

構造函數和析構函數:? __init__, __del__, __new__

python 輸入,讓我們從最常見的示例中來講,我們將定義我們自己的str類型,這種類型將比Python 中的str類型要強大許多。class String(object):

def __init__(self, val):

print "Creating instance of String"

self.val = val

def __del__(self):

print "Deleting instance of String"

python 類?#Python does support automatic garbage collection,

#Having destructor in your class may not be needed in most of the cases.

>>> st = String("Hello Python")

Creating instance of String

>>> del st

Deleting instance of String

python教程,從技術上來講 __init__ 是一個構造函數 , 它應該被使用來初始化實例屬性。然后誰真正意義上的來構造實例呢 ?

讓我們現在來重載__new__。

class String(object):

def __new__(cls, *args, **kargs):

print "Creating instance of String"

return super(String, cls).__new__(cls)

python async,def __init__(self, val):

print "Initiating instance of String"

self.val = val

def __del__(self):

print "Deleting instance of String"

>>> st = String("Hello Python")

append python、Creating instance of String

Initiating instance of String

__add__ 和 __sub__用來重載‘+’ , ‘-’運算符。

class String(object):

def __init__(self, val):

self.val = val

python 運算符重載。def __add__(self, other):

return self.val + '-' + other.val

def __sub__(self, other):

return "not supported"

>>> st1 = String("hello")

>>> st2 = String("Python")

python重載函數、>>> st1+st2

'hello-Python'

>>> st1 - st2

'not supported'

索引 : __getitem__ 和 __setitem__。

到目前為止 , 我們已經創建了用戶自定義類型string(字符串)和重載操作符 ‘+’和‘-’Python string(字符串) 支持索引,讓我們來檢查一下我們的‘string’類型是否支持索引呢 ?

python函數文檔說明、>>> print st1[0]

TypeError: 'String' object does not support indexing

__getitem__的幫助文檔在這里, 我們將會修改string的功能通過重載__setitem__。

class String(object):

def __init__(self, val):

self.val = val

python復合運算符、def __getitem__(self, index):

return self.val[index]

def __setitem__(self, index, val):

"hack to modify string"

self.val = list(self.val)

self.val[index] = val

python基本運算符,self.val = ''.join(self.val)

現在 ,‘string’類型索引應該起作用了。

>>> print st[0]

H

>>> print st[1]

e

Python struct、正如我之前承諾的一樣,我們的‘String’類型將會比Python的‘str’類型做更多的事情。

st[6] = 'C' #This is something won't work on python str

>>> print st.val

Hello Cython

讓我們試著打印‘String’類型的實例。

>>> print st? #It should print string value like python str

python函數重寫和重載、讓我們現在重載print(打印)操作符。

__str__ and __repr__

class String(object):

def __init__(self, val):

self.val = val

def __str__(self):

python decimal。return self.val

def __repr__(self):

return "This is String representation of " + self.val

現在打印 ‘String’類型應該會像Python‘str’類型一樣工作。

>>> print st???? #it calls __str__

Hello Python

python運算符重載。>>> st?????????? #It calls __repr__

This is String representation of Hello Python

擁有基本的string功能的完整的String類。

class String(object):

def __init__(self, val):

self.val = val

def __str__(self):

return self.val

def __repr__(self):

return "This is String representation of " + self.val

def __getitem__(self, index):

return self.val[index]

def __setitem__(self, index, val):

"hack to modify string"

self.val = list(self.val)

self.val[index] = val

self.val = ''.join(self.val)

def __add__(self, other):

return self.val + '-' + other.val

def __sub__(self, other):

return "not supported"

我們仍然希望能看到一些其他的操作符可以被重載并且被使用在設計模式中,我將在隨后的主題中介紹那些知識。

英文原文:http://www.pythonabc.com/operator-overloading-python/

譯者:wanghuan2054

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/176654.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息