python类变量作用域用法小结

来源:互联网 发布:淘宝卖家差评神回复 编辑:程序博客网 时间:2024/05/18 03:38

函数内部局部变量,此时aint为局部变量
class Test:
    def __init__(self):
        aint = 6
        print aint
#print self.aint报错
test = Test()
print test.aint

结果
6
报错
==================================
函数内声明类的属性


class Test:
    def __init__(self):
        self.aint = 6
        print self.aint

test = Test()
print test.aint

结果
6
6
=========================================
函数外声明的是类的属性
class Test:
    aint = 3
    def __init__(self):
        print self.aint

test = Test()
print test.aint

结果
3
3

原创粉丝点击