python中静态变量_Python中的静态变量和方法

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中,有两种定义静态方法的方法:

  1. Using the staticmethod()

    使用staticmethod()

  2. 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:

以下是一些重要的建议:

  1. 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.

    当我们要定义特定于类的某些行为或属性时,将使用静态变量和方法,这对于所有类对象都是通用的。

  2. If you look closely, for a static method we don't provide the argument self because static methods don't operate on objects.

    如果仔细观察,对于静态方法,我们不会提供self参数,因为静态方法不会对对象进行操作。

翻译自: https://www.studytonight.com/python/python-static-keyword

python中静态变量

Python中的静态变量 闭包是一个函数对象,它包含了一个函数与其相关的引用环境。我们可以利用闭包的特性来创建一个包含静态变量的函数。在Python中,虽然没有像其他编程语言那样直接支持静态变量的概念,但我们可以通过一些技巧来实现似的功能。在实际开发中,根据具体的需求使用场景,选择合适的方法来管理使用静态变量。这样,每次调用闭包函数时,它都可以访问修改静态变量的值,并且该值在不同的闭包实例之间是独立的。在Python中,变量是属于而不是实例的变量。的实例,我们可以看到静态变量的值在各个实例之间是共享的。 阅读详情

相关推荐

如何在 Windows 中实时打开字幕

在 Windows 操作系统中,你可以使用编程来实现实时字幕功能,以便在屏幕上显示即时的文字内容。下面是一个示例代码,展示了如何使用Python编程语言在Windows上实现实时字幕功能。这样,字幕窗口将会不断地显示系统当前时间作为字幕。你可以根据自己的需求修改主循环中的内容,以显示你想要的实时字幕。希望以上代码说明能够帮助你在Windows中实现实时字幕功能。函数创建了一个顶级窗口,并设置窗口的样式、位置大小。方法,用于在字幕窗口中显示指定的字幕内容。上述代码中,我们使用了Python的。

DevWizard的博客 3224

python ——静态变量

Python语言并不支持静态变量。因为Python是动态语言,不存在完全静态的变量。 Python中,静态成员变量称为变量,非静态成员变量称为实例变量 class A: ## 静态变量 a = 12 def __init__(self, a): ##成员变量 self.a = a print(A.a) # 12 print(A(0).a) # 0 静态变量是通过名.变量名 来访问的,成员变量是通过对象.变量名访问的。 class .

m0_61453287的博客 1万+

EasyFly1.05

免加密狗航模模拟器用,把音频模拟输入PPjoy

Python中的静态变量方法

如何在 Python 中创建静态变量或方法

HuntsBot的博客 6980

python 静态变量 静态方法 简介

目录 一、静态变量静态方法 二、静态变量示例 三、静态方法 一、静态变量静态方法 1、静态变量静态方法都属于的静态成员,它们与普通的成员变量成员方法不同,静态变量静态方法只属于定义它们的,而不属于某一个对象。 2、静态变量静态方法都可以通过对象进行访问。 二、静态变量示例 class StaticMenthod: count = 0 #静态变量,不需要显示的声明 def __init__(self): StaticMenthod.

whatday的专栏 6217

黑魔法:Python中的(静态)变量方法

Python中,是创建对象的模板,它允许我们定义一组属性方法来表示现实世界中的实体。在的内部,我们可以定义两种型的变量方法:实例变量静态变量/方法。本文将重点介绍静态变量方法,以及它们在Python中是如何工作的。

PythonWeb实践 1085

python静态变量静态方法

一、静态变量静态方法 1、静态变量静态方法都属于的静态成员,它们与普通的成员变量成员方法不同,静态变量静态方法只属于定义它们的,而不属于某一个对象。 2、静态变量静态方法都可以通过对象进行访问。 二、静态变量示例 class StaticMenthod: count = 0 #静态变量,不需要显示的声明 def __init__(self): StaticMentho...

ISmileLi的博客 2万+

python静态变量静态方法

一、静态变量静态方法 1、静态变量静态方法都属于的静态成员,它们与普通的成员变量成员方法不同,静态变量静态方法只属于定义它们的,而不属于某一个对象。 2、静态变量静态方法都可以通过对象进行访问。 二、静态变量示例 classStaticMenthod: count=0#静态变量,不需要显示的声明 def__init__(self): Stati...

我的笔记本 3348

python定义函数静态变量_python函数中使用静态变量方法

本文实例讲述了python函数中使用静态变量方法。分享给大家供大家参考。具体分析如下:在python函数(包括λ方法)中使用静态变量似乎是件不可能[Nothing is impossible]的事,但总有解决的办法,下面通过实现一个或函数的累加器来介绍一些较为非主流的方法方法一、通过的__init____call__方法class foo:def __init__(self, n...

weixin_35541322的博客 1250

python调用inca_python函数中使用静态变量方法

python函数中使用静态变量方法本文实例讲述了python函数中使用静态变量方法。分享给大家供大家参考。具体分析如下:在python函数(包括λ方法)中使用静态变量似乎是件不可能[Nothing is impossible]的事,但总有解决的办法,下面通过实现一个或函数的累加器来介绍一些较为非主流的方法方法一、通过的__init____call__方法class foo:...

weixin_39943383的博客 1697

python静态变量静态方法_python函数中使用静态变量方法

本文实例讲述了python函数中使用静态变量方法。分享给大家供大家参考。具体分析如下:在python函数(包括λ方法)中使用静态变量似乎是件不可能[Nothing is impossible]的事,但总有解决的办法,下面通过实现一个或函数的累加器来介绍一些较为非主流的方法方法一、通过的__init____call__方法class foo:def __init__(self, n...

weixin_34515649的博客 817

python函数_python函数中使用静态变量方法

本文实例讲述了python函数中使用静态变量方法。分享给大家供大家参考。具体分析如下:在python函数(包括λ方法)中使用静态变量似乎是件不可能[Nothing is impossible]的事,但总有解决的办法,下面通过实现一个或函数的累加器来介绍一些较为非主流的方法方法一、通过的__init____call__方法class foo:def __init__(self, n...

weixin_39860952的博客 219

python静态变量静态方法_从静态变量引用静态方法

Python2.7中,我想创建一个静态变量来存储运行封闭静态方法的结果。在我尝试了以下方法:class A:@staticmethoddef foo():return 1v = A.foo() # a static variableprint A.v返回错误:^{pr2}$但是,引用另一个静态变量是有效的:class B:@staticmethoddef foo():return 1cl...

weixin_39889544的博客 696

python静态变量静态方法_详解Python中的静态方法成员方法

前言因为Python的水平目前一直是处于能用阶段,平时写的脚本使用的Python的写法也比较的简单,没有写过稍微大一点的项目。对Python中的之间的组织关系,整个项目中之间如何耦合还缺乏认识。打算读一读别人写的Python代码来学习一下Python在工程中的应用,提升自己的技术水平。选取的Python代码是Python爬虫代码,github地址。这个代码刚好是符合跳出我的舒适区的水平的代...

weixin_39790102的博客 404

Python 静态变量

属性经常被用作常量的定义,这些常量在整个(及其所有实例)中都是不变的。例如,一个表示圆周率的常量PI可以定义为一个属性。# 使用Circle中的PI常量print(circle.area()) # 输出圆的面积,使用属性PIPython属性(静态变量)在很多情况下都是非常有用的,尤其是在定义常量、计数器、工厂方法全局状态等场景中。尽管Python并没有像某些其他语言那样明确地区分实例变量静态变量,但通过属性实例属性的概念,我们可以实现似的功能。

Python老吕的博客 3301

python菜鸟教程——的使用

静态变量的使用)

fenxiang_Hao的博客 729

Python】学习笔记day2

Python的基础语法,包括:动态型,注释,输入与输出.

C/C++领域,数据结构,linux,MySQl学习者 580

python静态变量静态方法_【开发者笔记】python中的方法(@classmethod)静态方法(@staticmethod)...

java、c#等高级语言中我们用static来定义静态方法静态变量,那么在python中如何定义静态方法静态变量呢。python提供了@classmethod@staticmethod来定义静态方法,刚接触的时候不太明白,Stack Overflow提供了一个比较方便理解的解释,Stack Overflow回答。但是看完还是不太理解,于是自己写了个实例:class stclass():d=...

weixin_33491377的博客 322

【附代码】Python 静态变量的实现方法(可多线程)

Python 并没有**静态变量**,但我们可以通过一些技巧来实现这样的效果。

ymzhu385的博客 1182

Python变量、方法(静态、、实例、全局、局部)超全详细解析

Python变量、方法(静态、、实例、全局、局部)超全详细解析 Python变量、方法(静态、、实例、全局、局部)超全详细解析 基础概念 全局变量-引用 全局变量-修改 nonlocal 中的各种变量 案例1 -静态方法方法 案例2-super 案例2 变量与实例变量 案例4–list变量 参考网址 基础概念 a、全局变量:在模块内、在所有函数外面、在clas...

ZOU19900101的博客 4728
上一篇: java登录页-视图界面_地图界面-Java集合
下一篇: jsp javabean_JSP JavaBean组件
cunfen6312
博客等级 码龄10年 86粉丝 0原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值