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++的靜態函式,他的一個特點是引數可以為空,同樣支援類名和物件兩種呼叫方式;
相關推薦
What is the 'cls' variable used for in Python classes?
在類方法中第一個引數名叫cls,這是一種預設的程式設計風格,並非強制,也可以叫其他名字。主要用於區分類方法和例項方法。 Function and method arguments: Always us
What is the 'cls' variable used for in Python classes?
在類方法中第一個引數名叫cls,這是一種預設的程式設計風格,並非強制,也可以叫其他名字。主要用於區分類方法和例項方法。 Function and method arguments: Always use self for the first argument to instance
Ask HN: What is the best smartphone for privacy conscious consumers in 2018?
Do you want privacy conscious or “off the grid”?For the former, Apple is the way to go; so far they haven’t done anything privacy-hostile and I personally
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
【轉載】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
killall doesn'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
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 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
What is the best country to get acquired in?
I'm trying to incorporate. I'll not raise any fund (I assume this why companies mainly others incorporate in the US) and I'm planning for an acquisitions.
Ask HN: What is the best IDE for python notebooks?
I really don't like the web interface for Jupyter. It is missing many essential features of an IDE.What alternatives are there to the jupyter web interface
What Is the Best Way to Improve Competition in Modern Capitalism?
This story is for Medium members.Continue with FacebookContinue with GoogleMedium curates expert stories from leading publishers exclusively for members (w
What Is the Maintenance Window for RDS and Redshift
The maintenance window occurs weekly, and DB instances can receive upgrades to the operating system (OS) or to the DB engine. AWS requires at l
1.1 What is the plug-in?
A game center, such as Lianzhong in China, supports hundreds of games such as Chess, Bridges, and Texas hold 'em poker. The game platform is made of all ga
what is the difference between Integer and int in java?
ive rim ever pan long () bool out com int is a primitive type, Variables of int type store the actual binary value for the Integer type y
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 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