DEV Community

Qingguang Zeng
Qingguang Zeng

Posted on

把class当做一个数据类型使用

我喜欢把class当做一个数据类型来使用。
我的方法是定义一个空的类,就像这样:

class data:
    pass
Enter fullscreen mode Exit fullscreen mode

然后,使用这个类,在里面存任何东西,例如:

man = data()
man.salary = 15000
man.name = "Man"
Enter fullscreen mode Exit fullscreen mode

然后,读取它们,就像这样:

print(man.salary + man.name)
Enter fullscreen mode Exit fullscreen mode

你有什么把类当做数据类型使用的好方法呢?

Top comments (1)

Collapse
 
awumiao profile image
Qingguang Zeng

I came across @oliverbennet is post "What's a Quickie Post?" too. So, I try it and write this post. Oh, my English is too bad!