最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

python类变量和实例变量区别

来源:动视网 责编:小采 时间:2020-11-03 23:19:05
文档

python类变量和实例变量区别

python类变量和实例变量区别:在Python Tutorial中对于类变量和实例变量是这样描述的: Generally speaking, instance variables are for data unique to each instance and class variables are for attributes and methods shared b
推荐度:
导读python类变量和实例变量区别:在Python Tutorial中对于类变量和实例变量是这样描述的: Generally speaking, instance variables are for data unique to each instance and class variables are for attributes and methods shared b
 在Python Tutorial中对于类变量和实例变量是这样描述的:

 Generally speaking, instance variables are for data unique to each instance and class variables are for attributes and methods shared by all instances of the class:

通常来说,实例变量是对于每个实例都独有的数据,而类变量是该类所有实例共享的属性和方法。(推荐学习:Python视频教程)

它们区别在于:实例变量为所属对象所私有,而类变量为所有对象所共有

类变量又叫全局变量,是属于类的特性,实例先找实例化变量,然后再去找类变量. 但是实例变量只是属于实例化的变量, 但是类变量也可以用实例去调用. 如果类变量有多重继承关系, 就需要按照指定的路线进行查找. 先看看代码理解下吧

class A:
 aa=10
 def __init__(self,a,b):
 self.a=a
 self.b=b
a=A(5,20)

print(a.a) #实例变量
print((a.aa))#实例读取类变量

#打印结果 
5
10

更多Python相关技术文章,请访问Python教程栏目进行学习!

文档

python类变量和实例变量区别

python类变量和实例变量区别:在Python Tutorial中对于类变量和实例变量是这样描述的: Generally speaking, instance variables are for data unique to each instance and class variables are for attributes and methods shared b
推荐度:
标签: 区别 实例 python
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top