Python类属性详解

python开发中,我们常常用到python的类,今天就通过实例和大家扒一扒类的属性,一起来看看吧。

 

类属性

 

1.类定义后就存在,而且不需要实例化

2.类属性使得相同类的不同实例共同持有相同变量

  

类属性实例

 

 attrb.py

  class TestCss:

    cssa = 'class-attribe'

    def __init__(self):

        self.a = 0

        self.b = 10

 

    def info(self):

        print('a:',self.a,'b:',self.b,'cssa:',TestCss.cssa)

 

    def define_a(self):

        self.c = 19

 

if __name__ == '__main__':

    # tc = TestCss()

    # tc.info()

    # tc.color = 'red'

    # print(tc.color)

    # tc = TestCss()

    # tca = TestCss()

    # tc.a = 100

    # tc.b = 200

    # tc.info()

    # tca.info()

    # tc = TestCss()

    # tc.define_a()

    # print(tc.c)

    tc = TestCss()

    tc.info()

    tca = TestCss()

    tc.info()

    TestCss.cssa = 0

    tc.info()

    tca.info()

 

程序的运行结果为:

 

 

原文链接:http://www.maiziedu.com/wiki/python/generic/

转载于:https://www.cnblogs.com/space007/p/5910137.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值