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公共变量

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543866.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

递归如何书写?

目录 第一步:首先你分析问题,要有递归的思路,知道要递归什么来解决问题。 第二步:先按照思路(第一层)写出函数的定义与函数体 第三步:根据函数的定义与函数体进一步确定需要的参数 第四步&a…

kotlin 判断数字_Kotlin程序可以逆转数字

kotlin 判断数字Given an integer number, we have to find reverse number and print it. 给定一个整数,我们必须找到反向数字并打印出来。 Example: 例: Input:Number: 12345Output:Reverse Number: 54321To find a reverse number – we use this f…

Python | 创建员工类别

Python-员工类代码 (Python - employee class code) # employee class code in Python# class definitionclass Employee:__id0__name""__gender""__city""__salary0# function to set data def setData(self,id,name,gender,city,salary):self.…

scala 字段覆盖_Scala中的字段覆盖

scala 字段覆盖Scala字段覆盖 (Scala field overriding) Overriding is the concept in which the child class is allowed to redefine the members of the parent class. Both methods and variables/ fields can be overridden in object-oriented programming. In Scala as…

python 散点图 分类_Python | 分类图

python 散点图 分类Visualizing different variables is also a part of basic plotting. Such variables can have different classes, for example, numerical or a category. Matplotlib has an important feature of Categorical Plotting. We can plot multiple categoric…

python 对角线矩阵_Python | 矩阵的对角线

python 对角线矩阵Some problems in linear algebra are mainly concerned with diagonal elements of the matrix. For this purpose, we have a predefined function numpy.diag(a) in NumPy library package which automatically stores diagonal elements in an array (a V…

二叉树祖先节点_二叉树的祖先

二叉树祖先节点Problem statement: 问题陈述: Given a Binary Tree and a target key, write a function that prints all the ancestors of the key in the given binary tree. 给定二叉树和目标键,编写一个函数,以打印给定二叉树中键的所有…

txt文本变为粗体_如何在PHP中使文本变为粗体?

txt文本变为粗体Sometimes we might want to display text with style. That its font, color, make it bold, italic, underlined and many more. Adding whatever style is all based on the message that we want to pass across or getting someones attention. 有时我们可…

CALayer精讲

CALayer精讲 CALayer包含在QuartzCore框架中,这是一个跨平台的框架,既可以用在iOS中又可以用在Mac OS X中。后面要学Core Animation就应该先学好Layer(层)。 我们看一下UIView与Layer之间的关系图(图片来源于网络&…

VSRE的完整形式是什么?

VSRE:预期回复非常短 (VSRE: Very Short Reply Expected) VSRE is an abbreviation of "Very Short Reply Expected". VSRE是“ Very Short Reply Expected”的缩写。 It is an expression, which is commonly used in the Gmail platform. It is writte…

rofl用什么播放_ROFL的完整形式是什么?

rofl用什么播放ROFL:笑在地板上滚动 (ROFL: Rolling On Floor Laughing) ROFL is an abbreviation of Rolling on Floor Laughing. ROFL is a very trendy internet slang between youngsters and used in text messaging, instant messaging, chatting, and social…

为什么只有根桥发送bpdu_BPDU的完整形式是什么?

为什么只有根桥发送bpduBPDU:网桥协议数据单元 (BPDU: Bridge Protocol Data Unit) BPDU is an abbreviation of the "Bridge Protocol Data Unit". BPDU是“网桥协议数据单元”的缩写 。 It is a data message in the form of a frame that used to exc…

什么叫穷举法?

穷举法的基本思想是根据题目的部分条件确定答案的大致范围,并在此范围内对所有可能的情况逐一验证,直到全部情况验证完毕。若某个情况验证符合题目的全部条件,则为本问题的一个解;若全部情况验证后都不符合题目的全部条件&#xf…

gif 格式 完整 检查_GIF的完整格式是什么?

gif 格式 完整 检查GIF:图形交换格式 (GIF: Graphics Interchange Format) GIF is an abbreviation of Graphics Interchange Format. It is extensively used for animations and still images on the World Wide Web. The image is set out is bitmap image and i…

Java基础_05

2019独角兽企业重金招聘Python工程师标准>>> 1:boolean运算符号 || 与 | && 与 &的区别。 Equals与innstanceof 1:java中的方法。方法的定义,参数、返回值、调用方式。 2:方法调用与参数传递、Static方…

账单cbl_CBL的完整形式是什么?

账单cblCBL:基于计算器的实验室 (CBL: Calculator-Based Laboratory) CBL is an abbreviation of "Calculator-Based Laboratory". CBL是“基于计算器的实验室”的缩写 。 It is a mobile data collection based piece of equipment. The process of col…

Android Studio 之下载安装

2019独角兽企业重金招聘Python工程师标准>>> 目录[-] 背景Android Studio VS Eclipse下载创建HelloWorld项目背景 相信大家对Android Studio已经不陌生了,Android Studio是Google于2013 I/O大会针对Android开发推出的新的开发工具,目前很多开…

c#格式化字float_C#中的float关键字

c#格式化字floatC#float关键字 (C# float keyword) In C#, float is a keyword which is used to declare a variable that can store a floating point value between the range of 1.5 x 10−45 to 3.4 x 1038. float keyword is an alias of System.Single. 在C&…

模拟UIWebView

2019独角兽企业重金招聘Python工程师标准>>> // // ViewController.m // 模拟UIWebView // // Created by dc0061 on 15/12/10. // Copyright © 2015年 dc0061. All rights reserved. //#import "ViewController.h"interface ViewController ()&…

ruby 将日期转化为时间_Ruby中的日期和时间类

ruby 将日期转化为时间Ruby数据和时间类 (Ruby Data and Time Classes) In any program written in any language, at any point of time you may need to fetch the date and time of the system at that instant. Ruby facilitates you with three classes related to Date a…