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'

 

上一篇:【转】android开发中关于模拟器emulation的常见问题


下一篇:新浪微博秀引用到jsp中显示空白