Python基于staticmethod裝飾器標(biāo)示靜態(tài)方法
英文文檔:
staticmethod(function)
Return a static method for function.
A static method does not receive an implicit first argument.
The @staticmethod form is a function decorator – see the description of function definitions in Function definitions for details.
It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class.
標(biāo)示方法為靜態(tài)方法的裝飾器
說明:
1. 類中普通的方法,實(shí)際上既可以被類直接調(diào)用也可以被類的實(shí)例對(duì)象調(diào)用,但是被實(shí)例對(duì)象調(diào)用的時(shí)候,要求方法至少有一個(gè)參數(shù),而且調(diào)用時(shí)會(huì)將實(shí)例對(duì)象本身傳給第一個(gè)參數(shù)
>>> class Student(object): def __init__(self,name): self.name = name def sayHello(lang): print(lang) if lang == 'en': print('Welcome!') else: print('你好!') >>> Student.sayHello <function Student.sayHello at 0x02AC7810> >>> a = Student('Bob') >>> a.sayHello <bound method Student.sayHello of <__main__.Student object at 0x02AD03F0>> >>> Student.sayHello('en') # 類調(diào)用的時(shí)候,將'en'傳給了lang參數(shù) en Welcome! >>> a.sayHello() # 類實(shí)例對(duì)象調(diào)用的時(shí)候,將對(duì)象本身自動(dòng)傳給了lang參數(shù),不能再接收參數(shù) <__main__.Student object at 0x02AD03F0> 你好! >>> a.sayHello('en') Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> a.sayHello('en') TypeError: sayHello() takes 1 positional argument but 2 were given
2. staticmethod函數(shù)功能就是將一個(gè)方法定義成類的靜態(tài)方法,正確的方法是使用 @staticmethod裝飾器,這樣在實(shí)例對(duì)象調(diào)用的時(shí)候,不會(huì)把實(shí)例對(duì)象本身傳入靜態(tài)方法的第一個(gè)參數(shù)了。
# 使用裝飾器定義靜態(tài)方法 >>> class Student(object): def __init__(self,name): self.name = name @staticmethod def sayHello(lang): print(lang) if lang == 'en': print('Welcome!') else: print('你好!') >>> Student.sayHello('en') #類調(diào)用,'en'傳給了lang參數(shù) en Welcome! >>> b = Student('Kim') #類實(shí)例對(duì)象調(diào)用,不再將類實(shí)例對(duì)象傳入靜態(tài)方法 >>> b.sayHello() Traceback (most recent call last): File "<pyshell#71>", line 1, in <module> b.sayHello() TypeError: sayHello() missing 1 required positional argument: 'lang' >>> b.sayHello('zh') #類實(shí)例對(duì)象調(diào)用,'zh'傳給了lang參數(shù) zh 你好!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python中的classmethod與staticmethod
- 詳解Python中@staticmethod和@classmethod區(qū)別及使用示例代碼
- Python 類方法和實(shí)例方法(@classmethod),靜態(tài)方法(@staticmethod)原理與用法分析
- Python中staticmethod和classmethod的作用與區(qū)別
- python的staticmethod與classmethod實(shí)現(xiàn)實(shí)例代碼
- 基于python中staticmethod和classmethod的區(qū)別(詳解)
- 詳解python中靜態(tài)方法staticmethod用法
相關(guān)文章
python requests更換代理適用于IP頻率限制的方法
今天小編就為大家分享一篇python requests更換代理適用于IP頻率限制的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python Shiny庫創(chuàng)建交互式Web應(yīng)用及高級(jí)功能案例
Shiny是一個(gè)基于Python的交互式Web應(yīng)用框架,專注于簡(jiǎn)化Web應(yīng)用的開發(fā)流程,本文將深入探討Shiny庫的基本用法、高級(jí)功能以及實(shí)際應(yīng)用案例,以幫助開發(fā)者充分發(fā)揮Shiny在Web應(yīng)用開發(fā)中的優(yōu)勢(shì)2023-12-12Python Logging 日志記錄入門學(xué)習(xí)
這篇文章主要介紹了Python Logging 日志記錄入門學(xué)習(xí),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06CNN的Pytorch實(shí)現(xiàn)(LeNet)
本文主要從CNN的Pytorch實(shí)現(xiàn)庫導(dǎo)入,模型定義,數(shù)據(jù)加載、處理,模型訓(xùn)練,代碼匯總等方面入手介紹,運(yùn)用代碼講解相關(guān)內(nèi)容非常的詳細(xì),大家如果有需要了解相關(guān)知識(shí)的可以參考這篇文章2021-09-09解析python調(diào)用函數(shù)加括號(hào)和不加括號(hào)的區(qū)別
這篇文章主要介紹了python調(diào)用函數(shù)加括號(hào)和不加括號(hào)的區(qū)別,不帶括號(hào)時(shí),調(diào)用的是這個(gè)函數(shù)本身 ,是整個(gè)函數(shù)體,是一個(gè)函數(shù)對(duì)象,不須等該函數(shù)執(zhí)行完成,具體實(shí)例代碼跟隨小編一起看看吧2021-10-10面向新手解析python Beautiful Soup基本用法
這篇文章主要介紹了面向新手解析python Beautiful Soup基本用法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07