python公共变量_Python中的公共变量
python公共變量
By default all numbers, methods, variables of the class are public in the Python programming language; we can access them outside of the class using the object name.
默認(rèn)情況下,該類的所有數(shù)字,方法和變量在Python編程語言中都是公共的。 我們可以使用對象名稱在類外部訪問它們。
Consider the given example:
考慮給定的示例:
Here, we have two variables name and age both are initializing with default values ("XYZ" and 0) while declaring the object using __init__ method.
在這里,我們有兩個變量name和age都使用默認(rèn)值( “ XYZ”和0 )初始化,同時使用__init__方法聲明對象。
Later, in the program, outside of the class definition – we are assigning some values ("Amit" and 21) to name and age, that is possible only if variables are public.
后來,在程序中,在類定義之外–我們?yōu)閚ame和age分配了一些值( “ Amit”和21 ),只有在變量為public的情況下才可能。
Example:
例:
# Python example for public variables class person:def __init__(self):# default valuesself.name = "XYZ"self.age = 0def printValues(self):print "Name: ",self.name print "Age : ",self.age# Main code # declare object p = person() # print p.printValues();# since variables are public by default # we can access them directly here p.name = "Amit" p.age = 21 # print p.printValues ();Output
輸出量
Name: XYZ Age : 0 Name: Amit Age : 21翻譯自: https://www.includehelp.com/python/public-variables.aspx
python公共變量
總結(jié)
以上是生活随笔為你收集整理的python公共变量_Python中的公共变量的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python字符串垂直输出加循环_将漂亮
- 下一篇: python radians函数_Pyt