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.
默认情况下,该类的所有数字,方法和变量在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都使用默认值( “ 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.
后来,在程序中,在类定义之外–我们为name和age分配了一些值( “ Amit”和21 ),只有在变量为public的情况下才可能。
Example:
例:
# Python example for public variables
class person:
def __init__(self):
# default values
self.name = "XYZ"
self.age = 0
def 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公共变量
python公共变量_Python中的公共变量相关推荐
- python中静态变量_Python中的静态变量和方法
python中静态变量 Defining static variable and method is a common programming concept and is widely used i ...
- python 分类变量转为哑变量_Python中的虚拟变量(dummyvariables)
虚拟变量(dummy variables) 虚拟变量,也叫哑变量和离散特征编码,可用来表示分类变量.非数量因素可能产生的影响. ① 离散特征的取值之间有大小的意义 例如:尺寸(L.XL.XXL) 离散 ...
- python私有变量_python中的私有变量
classTest1:deff1(self): self.name="张三"self.__age = 20 #使用名称变形实现私有变量 print(self.name)print( ...
- python赋值语句格式_Python中变量和变量赋值的几种形式
动态类型的语言 python是动态类型的语言,不需要声明变量的类型. 实际上,python中的变量仅仅只是用来保存一个数据对象的地址.无论是什么数据对象,在内存中创建好数据对象之后,都只是把它的地址保 ...
- macbook配置java环境变量_Mac系统中如何配置JDK环境变量?Mac中JDK环境变量配置教程...
Mac系统中如何配置JDK环境变量?在使用mac系统的电脑时,如何进行设置JDK环境变量呢?接下来的文章中小编将会带来详细的介绍,希望对您有所帮助. Mac中JDK环境变量配置教程 1.访问Oracl ...
- python变量需要声明吗_python中可以声明变量类型吗
变量(variable)是Python语言中一个非常重要的概念.变量的主要作用就是为Python程序中的某个值起一个名字.类似于"张三"."李四"." ...
- python变量类型声明_python中可以声明变量类型吗
变量(variable)是Python语言中一个非常重要的概念.变量的主要作用就是为Python程序中的某个值起一个名字.类似于"张三"."李四"." ...
- python如何复制一个变量_Python中变量、赋值、浅拷贝、深拷贝
在理解浅拷贝和深拷贝之前,首先要理解学习一下变量在Python中是怎样存储的: 变量的类型是分值引用与地址引用两种. python的一切变量都是对象,变量的存储,采用了地址引用的方式,存储的只是一个变 ...
- python 类中静态变量_Python中的类或静态变量
python 类中静态变量 Python类/静态变量 (Python Class / Static Variables) Class or Static variables are class-rel ...
最新文章
- LeetCode简单题之差的绝对值为 K 的数对数目
- 参加web前端培训要学哪些知识
- freemarker中js里面取字符串,换行导致报错的解决办法
- C#.NET禁止一个程序启动多个实例
- 彩色图像分割MATLAB代码
- 华为3Com Quidway 2116-SI
- [单选题]?php echo ceil(2.1/0.7); ?
- TypeError: 'float' object is not iterable
- 数据操作类:增删改查
- Unable to compile class for JSP 的可能原因
- python批量更改图片尺寸(保持长度和高度的长短关系)
- netty 使用阻塞发送_大数据、分布式都用到了的Netty,这几大核心知识你一定要看看!...
- SAP OLE中常用的一些方法和属性
- Kuma初步学习笔记-universal 模式
- PPT模板 | 湖南农业大学汇报通用PPT模板
- 高并发系统的限流算法与实现
- Java项目部署到云服务器的思路
- 二、Android BCB的作用
- 若依前后端分离框架验证码的学习
- 格式化时间戳,时间加一天
热门文章
- android搭建opencv开发环境,Android Studio搭建opencv开发环境
- 判断按键值_Pygame(九)按键事件(2)
- Linux基础(iptables与firewalld防火墙)
- php使用jasperreport,php-报表引擎指南(Pentaho,JasperReports,BIRT)
- Kewail-邮件短信接口的基础教程
- MySQL 直接存储图片并在 html 页面中展示,点击下载
- 【项目经验】如何用TexturePacker Physicseditor开发游戏
- jsonp的简单学习
- Exchange 2003升级至Exchange 2007
- Angular开发准备