What is the 'cls' variable used for in Python classes?
在類方法中第一個引數名叫cls
,這是一種預設的程式設計風格,並非強制,也可以叫其他名字。主要用於區分類方法和例項方法。
Function and method arguments:
Always use self for the first argument to instance methods.
Always use cls for the first argument to class methods.
python的cls,self,classmethod,staticmethod:
python類裡會出現這三個單詞,self和cls都可以用別的單詞代替,類的方法有三種,
一是通過def定義的
普通的一般的,需要至少傳遞一個引數,一般用self,這樣的方法必須通過一個類的例項去訪問,類似於c++中通過物件去訪問;
二是在def前面加上@classmethod,這種類方法的一個特點就是可以通過類名去呼叫,但是也必須傳遞一個引數,一般用cls表示class,表示可以通過類直接呼叫;
三是在def前面加上@staticmethod,這種類方法是靜態的類方法,類似於c++的靜態函式,他的一個特點是引數可以為空,同樣支援類名和物件兩種呼叫方式;
參考連結:
https://stackoverflow.com/questions/4613000/what-is-the-cls-variable-used-for-in-python-classes
相關推薦
What is the 39;cls39; variable used for in Python classes?
在類方法中第一個引數名叫cls,這是一種預設的程式設計風格,並非強制,也可以叫其他名字。主要用於區分類方法和例項方法。 Function and method arguments: Always use self for the first argument to instance
What is the 'cls' variable used for in Python classes?
在類方法中第一個引數名叫cls,這是一種預設的程式設計風格,並非強制,也可以叫其他名字。主要用於區分類方法和例項方法。 Function and method arguments: Always us
Ask HN: What is the best Twitter sentiment analysis tool for a small business
I run a small business which primarily sells online. Can the HN community help me with a tool ? Where I can figure out what people are thinking about my br
killall doesn39;t kill all and rarely kills, what is the command for then?
轉載自https://askubuntu.com/questions/271028/killall-doesnt-kill-all-and-rarely-kills-what-is-the-command-for-then Q: I occasionally use the kil
Is the MIME type 39;image/jpg39; the same as 39;image/jpeg39;?
xpl nis text www. nco ima www see ref https://stackoverflow.com/questions/33692835/is-the-mime-type-image-jpg-the-same-as-image-jpeg
What is the difference between Kill and Kill -9 command in Unix?
data esp osi lin mil print ren win sku w difference kill -9 pid and kill pid command - Ask Ubuntu https://askubuntu.com/questions/7918
UVA10056 - What is the Probability ?(概率)
ant scanf org import 1.0 key align review soft UVA10056 - What is the Probability ?(概率) 題目鏈接 題目大意:有n個人玩遊戲,一直到一個人勝出之後遊戲就能夠結束,要不然就一直從第
What is the Windows Integrity Mechanism?(什麽是Windows完整性機制)
ech 管理員 tac p s configure hierarchy 重要 issue ide The Windows integrity mechanism is a core component of the Windows security architectu
【轉載】What is the difference between authorized_keys and known_hosts file for SSH?
led accounts dep protocol wide HERE data round enc The known_hosts file lets the client authenticate the server, to check that it isn‘t c
What is the difference between static func and class func in Swift?
truct computed per value subclass guid between tab odi Special Kinds of Methods Methods associated with a type rather than an instance
【轉】What is the minimum version of Red Hat Enterprise Linux that supports Intel® Omni-Path? (OPA)
原文連結:https://access.redhat.com/solutions/2803981 What is the minimum version of Red Hat Enterprise Linux that supports Intel® Omni-Path? (OPA) &nb
What is the difference between iface eth0 inet manual and iface eth0 inet static?
iface eth0 inet static: Defines a static IP address for eth0 iface eth0 inet manual :To create a network interface without an IP address at a
What is the difference between book depreciation and tax depreciation?
Generally, the difference involves the "timing" of the depreciation expense on a company's financial statementsversus the depreciati
Pandas isna() and isnull(), what is the difference?
isnull is an alias for isna. Literally in the code source of pandas: isnull = isna Indeed: >>> pd.isnull <function isna at 0x
What is the meaning of "Error 10000 (sugg. bt 0x0, driver bt 0x0, host bt 0x1)"?
Environment Red Hat Enterprise Linux (RHEL) 6 Red Hat Enterprise Linux (RHEL) 5 Issue What is the meaning of "Error 10000 (sugg. bt 0
What is the difference between PPTP, L2TP/IPsec, SSTP, IKEv2, and OpenVPN?
相關文章: iOS10把PPTP協定拿掉了,VPN協定只剩下L2TP、IPSEC、IKEv2。IKEv2 從 iOS 8.0 版開始支援。 PPTP和L2TP都使用PPP協議對資料進行封裝,然後新增附加包頭用於資料在網際網路絡上的傳輸。儘管兩個協議非常相似,但是仍存在以下幾方面的不同: 1.PPT
What is the best way to paginate results in SQL Server
先了解 MySQL server side paging: First off, don’t have a separate server script for each page, that is just madness. Most applications implement pagination v
Ask HN: What is the best Gmail client alternative?
Gmail.com totally sucks now since they've pushed the new UI to everyone (no revert back option). We all hate it, it's confusing, can't even find out if I h
What is the difference between Machine Learning and Artificial Intelligence?
Artificial Intelligence and Machine Learning are two terms related to the world of computer science that can be heard a lot these days. These technologies
Ask HN: What is the average rate of corporation tax paid by YC Companies?
There is a lot of focus by SV figures these days about philanthropy for social things like schooling, basic income etc.As someone who lives in Ireland and