python 类中静态变量_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 类中静态变量

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值