码迷,mamicode.com
首页 > 编程语言 > 详细

python中类变量和实例变量

时间:2019-01-19 16:31:22      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:gen   示例   share   The   each   pre   ref   variable   and   

1. 类变量和实例变量

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:

通常来说,实例变量是对于每个实例都独有的数据,而类变量是该类所有实例共享的属性和方法。

其实我更愿意用类属性和实例属性来称呼它们,但是变量这个词已经成为程序语言的习惯称谓。一个正常的示例是:

class Dog:

    kind = ‘canine‘         # class variable shared by all instances

    def __init__(self, name):
        self.name = name    # instance variable unique to each instance
Dog中,类属性kind为所有实例所共享;实例属性name为每个Dog的实例独有。

python中类变量和实例变量

标签:gen   示例   share   The   each   pre   ref   variable   and   

原文地址:https://www.cnblogs.com/CK85/p/10291827.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!