Python中的靜態函式的用法
Python中也存在靜態函式,檢視文件之後,文件解釋這個功能跟Java或者C++的功能相同的。
總結Python中的靜態函式,先看一段自己寫的簡單的程式碼:
class MyClass:
@staticmethod
def Func():
print("static function inpython")
class MyClass1:
def Func(self):
print("non-static function inpython")
obj1 = MyClass()
obj2 = MyClass1()
obj1.Func()
obj2.Func()
MyClass.Func()
MyClass1.Func()
上面的程式碼中定義了兩個類,定義了兩個相應類的例項化物件。兩個類中的方法有一個是靜態方法,另一個則是一般的方法。分別通過例項化物件和類對齊方法進行呼叫,程式的執行結果如下:
[email protected]:/mnt/e/01_workspace/02_programme_language/03_python/OOP/2017/08/12$python staticmethod.py
static function inpython
non-staticfunction in python
static function inpython
Traceback (mostrecent call last):
File "staticmethod.py", line 17, in<module>
MyClass1.Func()
TypeError: unboundmethod Func() must be called with MyClass1 instance as first argument (gotnothing instead)
從上面得出結論:
1,靜態方法可以不帶self引數
2,靜態方法可以通過類進行呼叫,但是一般方法不行
一個更好地示範程式碼如下:
class MyClass:
@staticmethod
def F1():
print("Functiom 1")
def F2(self):
MyClass.F1()
def F3(self):
MyClass.F2()
obj = MyClass()
obj.F1()
obj.F2()
obj.F3()
程式的執行結果:
[email protected]:/mnt/e/01_workspace/02_programme_language/03_python/OOP/2017/08/12$python s1.py
Functiom 1
Functiom 1
Traceback (most recentcall last):
File "s1.py", line 15, in<module>
obj.F3()
File "s1.py", line 10, in F3
MyClass.F2()
TypeError: unboundmethod F2() must be called with MyClass instance as first argument (got nothinginstead)
從上面看,靜態方法比較便於類內部的呼叫,他與類的物件無關,僅僅與類有關。如果想要上面的程式碼執行通過,修改如下:
class MyClass:
@staticmethod
def F1():
print("Functiom 1")
def F2(self):
MyClass.F1()
def F3(self):
self.F2()
obj = MyClass()
obj.F1()
obj.F2()
obj.F3()
程式執行結果:
[email protected]:/mnt/e/01_workspace/02_programme_language/03_python/OOP/2017/08/12$python s1.py
Functiom 1
Functiom 1
Functiom 1
相關推薦
Python中split()函式用法和例項
一、描述 split()通過指定分隔符對字串進行切片,如果引數num 有指定值,則僅分隔 num 個子字串 函式形式:str.split(str="", num=string.count(str)) 引數: str -- 分隔符,預設為所有的空字元,包括空格、換行(\n)、製表
python 中 print 函式用法總結
出自:http://www.cnblogs.com/graceting/p/3875438.html Python 思想: “一切都是物件!” 在 Python 3 中接觸的第一個很大的差異就是縮排是作為語法的一部分,這和C++等其他語言確實很不一樣,所以要小心
Python中format函式用法說明
格式描述 %%百分號標記 %c字元及其ASCII碼 %s字串 %d有符號整數(十進位制) %u無符號整數(十進位制) %o無符號整數(八進位制) %x無符號整數(十六進位制) %X無符號整數(十六進位
Python中zip()函式用法舉例
定義:zip([iterable, ...]) zip()是Python的一個內建函式,它接受一系列可迭代的物件作為引數,將物件中對應的元素打包成一個個tuple(元組),然後返回由這些 tuples組成的list(列表)。若傳入引數的長度不等,則返回list的長度和引數
python中range()函式用法
1、range範圍: range 範圍是左閉右開區間, range(i,j) 相當於 [i,j)。 程式設計時要注意邊界問題 >>> for i in range(6,12): print(i) 6 7 8 9 10 11 2、關於引數 >>> range(1,5
python中encode()函式的用法
python字串函式用法大全連結 encode()函式 描述:以指定的編碼格式編碼字串,預設編碼為 'utf-8'。 語法:str.encode(encoding='utf-8', errors='strict') -> bytes (獲得by
python中count()函式的用法
python字串函式用法大全連結 count()函式 描述:統計字串裡某個字元出現的次數。可以選擇字串索引的起始位置和結束位置。 語法:str.count("char", start,end)
python中center()函式的用法
python字串函式用法大全連結 center()函式 描述:返回一個長度為width,兩邊用fillchar(單字元)填充的字串,即字串str居中,兩邊用fillchar填充。若字串的長度大於width,則直接返回字串str 語法:str.center(width , "fillcha
python中casefold()函式的用法
python字串函式用法大全連結 casefold()函式 描述:將字串中的所有大寫字母轉換為小寫字母。 語法:str.casefold() -> str 返回字串 程式示例: str1 = "I Love Python" str2 = "Groß
python中startswith()函式的用法
python字串函式用法大全連結 startswith()函式 描述:判斷字串是否以指定字元或子字串開頭。 語法:str.endswith("suffix", start, end) 或 str[start,end].endswith("suffix") 用
python中endswith()函式的用法
python字串函式用法大全連結 endswith()函式 描述:判斷字串是否以指定字元或子字串結尾。 語法:str.endswith("suffix", start, end) 或 str[start,end].endswith("suffix") 用於判
python中expandtabs()函式的用法
python字串函式用法大全連結 expandtabs()函式 描述:返回一個字串的副本。使原字串中的製表符("\t")的使用空間變大。使用空格來擴充套件空間。 語法: str.expandtabs(tabsize=8) —> str 返回字串 t
python中decode()函式函式的用法
python字串函式用法大全連結 decode()函式 描述:以 encoding 指定的編碼格式解碼字串,預設編碼為字串編碼。 encoding ——要使用的編碼,如:utf-8,gb2312,cp936,gbk等。 errors ——設定不同解碼
Python雜談 | (三) Python中flatten()函式的用法
目錄 一、簡介 二、用於array 三、用於mat 四、用於列表 一、簡介 Python的flatten()函式位於numpy庫中,只適用於array或mat這兩個NumPy物件,普通的列表不行。 二、用於array 三、用於mat
python中join函式的用法
這個函式可以對字串按照某種方式進行拼接,比如你要在三個字母中間都新增一個特定字元,就可以用這個函式實現 result = '*'.join(['A','B','C']) print(result) #A*B*C 當然join後傳入的引數是一個string型別同樣也是可以的
【轉】Python關鍵語法-閉包:函式中的函式用法例項
本文例項講述了Python閉包的用法。分享給大家供大家參考,具體如下: Python函式中也可以定義函式,也就是閉包。跟js中的閉包概念其實差不多,舉個Python中閉包的例子。 def make_adder(addend): def adder(augend
Python中sorted函式的用法
[轉].Python中sorted函式的用法 【Python】 sorted函式 我們需要對List、Dict進行排序,Python提供了兩個方法
python中zip()函式的用法
版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/csdn15698845876/article/details/73411541 zip函式的原型為:zip([iterable, …]) 引數iterable為可迭代的物件
Python中sorted()函式的高階用法詳解
sorted()函式的作用是對物件進行排序 函式函式格式: sorted(iterable,key,reverse),key引數可傳入一個自定義函式 下邊通過具體例子說明sorted的具體用法: 一維陣列,直接排序即可: #!/usr/bin/env python # -*-
python中lower()函式的用法
lower()函式 描述:將字串中的所有大寫字母轉換為小寫字母。 語法:str.lower() -> str 返回字串 程式示例: str1 = "I Love Python" str2 = "Groß - α" #德語 大寫α print(str1.c