python中静态变量
Defining static variable and method is a common programming concept and is widely used in C++, Java, Php and many other programming languages for creating class variables and methods that belong to the class and are shared by all the objcts of the class.
定义静态变量和方法是一种常见的编程概念,并广泛用于C ++,Java,Php和许多其他编程语言中,以创建属于该类并由该类的所有对象共享的类变量和方法。
In Python, there is no special keyword for creating static variables and methods. Python follows a different but simple approach for defining static variables and methods which we will learn in this tutorial.
在Python中,没有用于创建静态变量和方法的特殊关键字。 Python采用了一种不同但简单的方法来定义静态变量和方法,我们将在本教程中学习该方法。
Python中的类或静态变量 (Class or Static Variables in Python)
Class or Static variables are the variables that belong to the class and not to objects. Class or Static variables are shared amongst objects of the class. All variables which are assigned a value in the class declaration are class variables. And variables which are assigned values inside class methods are instance variables.
类或静态变量是属于类而不属于对象的变量。 类或静态变量在类的对象之间共享。 在类声明中分配了值的所有变量都是类变量。 在类方法中分配值的变量是实例变量。
Let's have an example to understand this:
让我们举一个例子来理解这一点:
In the above program, cat is a class variable because it is defined outside of all the class methods and inside the class definition and type is an instance variable as it is defined inside a method.
在上面的程序中, cat是一个类变量,因为它是在所有类方法的外部定义的,并且在类定义内部,而type是在方法内部定义的实例变量。
This is confirmed using the print statement where the cat variable is referenced using the class name Shape while the type variable is referenced using the different object references.
使用print语句可以确认这一点,其中,使用类名Shape来引用cat变量,而使用不同的对象引用来引用type变量。
The above example shows a scenario where there are different shape objects each belonging to the same category which is Geometrical but are of different types, so each object of the class have the same category which we have made the class variable and the type variable is different for all the objects hence it is an instance variable.
上面的示例显示了一个场景,其中存在不同的形状对象,每个形状对象属于同一类别,属于几何类别,但类型不同,因此该类的每个对象都具有相同的类别,这使我们将类变量设为不同,并且type变量不同对于所有对象,因此它是一个实例变量。
NOTE: Python allows providing same variable name for a class/static variable and an instance variable. But we would recommend you to not to provide same name variables to these variables to avoid confusion.
注意: Python允许为类/静态变量和实例变量提供相同的变量名。 但是我们建议您不要为这些变量提供相同的名称变量,以免造成混淆。
Python中的静态方法 (Static Methods in Python)
Just like static variables, static methods are the methods which are bound to the class rather than an object of the class and hence are called using the class name and not the objects of the class.
就像静态变量一样,静态方法是绑定到类而不是类的对象的方法,因此使用类名而不是类的对象进行调用。
As static methods are bound to the class hence they cannot change the state of an object.
由于静态方法绑定到该类,因此它们无法更改对象的状态。
To call a static method we don't need any class object it can be directly called using the class name.
要调用静态方法,我们不需要任何类对象,可以使用类名称直接调用它。
In python there are two ways of defining a static method:
在python中,有两种定义静态方法的方法:
Using the
staticmethod()使用
staticmethod()Using the
@staticmethod使用
@staticmethod
使用staticmethod()定义静态方法 (Define static method using staticmethod())
Let's take an example to see how this is done:
让我们来看一个例子,看看如何做到这一点:
class Shape:
def info(msg):
# show custom message
print(msg)
print("This class is used for representing different shapes.")
# create info static method
Shape.info = staticmethod(Shape.info)
Shape.info("Welcome to Shape class")
Welcome to Shape class This class is used for representing different shapes.
欢迎使用Shape类该类用于表示不同的形状。
In the program above, we declared the info method as static method outside the class using the staticmethod() function approach and after that we were able to call the info() method directly using the class Shape.
在上面的程序中,我们使用staticmethod()函数方法将info方法声明为类之外的staticmethod()方法,之后,我们可以使用Shape类直接调用info()方法。
使用@staticmethod定义静态方法 (Define static method using @staticmethod)
Let's take an example to see how this is done:
让我们来看一个例子,看看如何做到这一点:
class Shape:
@staticmethod
def info(msg):
# show custom message
print(msg)
print("This class is used for representing different shapes.")
Shape.info("Welcome to Shape class")
Welcome to Shape class This class is used for representing different shapes.
欢迎使用Shape类该类用于表示不同的形状。
Using @staticmethod is a more modern approach of defining static method and we recommend this approach.
使用@staticmethod是定义静态方法的更现代的方法,我们建议使用此方法。
关于静态变量和方法要记住的要点 (Points to Remember about static variable and methods)
Following are some important take aways:
以下是一些重要的建议:
Static variable and methods are used when we want to define some behaviour or property specific to the class and which is something common for all the class objects.
当我们要定义特定于类的某些行为或属性时,将使用静态变量和方法,这对于所有类对象都是通用的。
If you look closely, for a static method we don't provide the argument
selfbecause static methods don't operate on objects.如果仔细观察,对于静态方法,我们不会提供
self参数,因为静态方法不会对对象进行操作。
翻译自: https://www.studytonight.com/python/python-static-keyword
python中静态变量
3224




被折叠的 条评论
为什么被折叠?



