詹惠儿

2019-01-03   阅读量: 584

数据分析师

python怎么实现示例变量

扫码加入数据分析学习群

=实例变量(或属性)
在Python中,实例变量是其值在构造函数或方法中使用self分配的变量。

类变量是其值在类中赋值的变量。

# Python program to show that the variables with a value

# assigned in class declaration, are class variables and

# variables inside methods and constructors are instance

# variables.

# Class for Computer Science Student

class CSStudent:

# Class Variable

stream = 'cse'

# The init method or constructor

def __init__(self, roll):

# Instance Variable

self.roll = roll

# Objects of CSStudent class

a = CSStudent(101)

b = CSStudent(102)

print(a.stream) # prints "cse"

print(b.stream) # prints "cse"

print(a.roll) # prints 101

# Class variables can be accessed using class

# name also

print(CSStudent.stream) # prints "cse"

添加CDA认证专家【维克多阿涛】,微信号:【cdashijiazhuang】,提供数据分析指导及CDA考试秘籍。已助千人通过CDA数字化人才认证。欢迎交流,共同成长!
0.0000 0 3 关注作者 收藏

评论(0)


暂无数据

推荐课程

推荐帖子