python--类

类的定义

# 定义类
class Luffy:
    school = 'luffy'    # 数据属性
    def learn(self):
        print('is learning')
    def eat(self):      # 函数属性
        print('is eating')

print(Luffy.__dict__)
----------------------------------------
{'__module__': '__main__', 'school': 'luffy', 'learn': <function Luffy.learn at 0x005C9B70>, 'eat': <function Luffy.eat at 0x005C9BB8>, '__dict__': <attribute '__dict__' of 'Luffy' objects>, '__weakref__': <attribute '__weakref__' of 'Luffy' objects>, '__doc__': None}

与函数不同

类定义完以后,就产生了名称空间,而函数在调用的时候才会产生

def test():
    a = 1
    print('hah')
    return a

print(test.__dict__)
----------------------------
{}

类的增删查改

# 内部名称空间
print(Luffy.__dict__)

#
print(Luffy.__dict__['school'])
print(Luffy.school)

#
Luffy.county = 'China'

#
del Luffy.school

#
Luffy.school = 'oldboy'

 

posted @ 2019-08-24 15:51  lalaxing  阅读(271)  评论(0编辑  收藏  举报