栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 类中静态变量

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 类中静态变量

python 类中静态变量

Python类/静态变量 (Python Class / Static Variables)

Class or Static variables are class-related variables that are shared among all objects but the instance or non-static variable is unique for each object. In Python, there is no need for the keyword static to make a class variable but the static keyword is used to make a class or static variables in other programming languages like C++, C, Java, etc. The variable defined on the class level that is declared inside the class but outside any method in Python is static.

类或静态变量是在所有对象之间共享的与类相关的变量,但是实例或非静态变量对于每个对象都是唯一的。 在Python中,不需要使用关键字static来创建类变量,而是使用static关键字来创建类或其他编程语言(例如C ++,C,Java等)中的静态变量。在类级别定义的变量在类内部声明,但在Python中的任何方法外部都是静态的。

Let's look at an example for a better understanding of the static variable that returns some details about the intern of "includehelp" in Python.

让我们看一个示例,以更好地理解静态变量,该变量返回有关Python中“ includehelp”的内部实习生的一些详细信息。

Example for the class or static variable

类或静态变量的示例

# class definition
class intern:
    site_name='Includehelp'
    field='Technical content writer'
    
    def __init__(self,name,programming_language):
        self.name=name
        self.programming_language=programming_language

# object of the intern class
x = intern('Bipin Kumar','Python') 

# printing the variables values
print('Site name: ',x.site_name)
print('Field of interest: ',x.field)
print('Name of intern: ',x.name)
print('Language: ',x.programming_language)

Output

输出量

Site name:  Includehelp
Field of interest:  Technical content writer
Name of intern:  Bipin Kumar
Language:  Python

Explanation:

说明:

In the above example, site_name and field is a class or static variable which is declared inside the class "intern" but outside of the __init__ method and name and programming_language are instance variables. Here, x is the object of the class "intern" which has two arguments, one is the name of an intern and another one is the programming language.

在上面的示例中, site_name和field是一个类或静态变量 ,在类“ intern”内声明,但在__init__方法之外,并且name和programming_language是实例变量。 在此, x是“ intern”类的对象,该类具有两个参数,一个是实习生的名称,另一个是编程语言。

翻译自: https://www.includehelp.com/python/class-or-static-variables.aspx

python 类中静态变量

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号