Python中的一切皆对象的理解

Python有一个重要的概念,一切皆对象。

一切都可以赋值给变量:

内置类型赋值:

1 >>> a=1
2 >>> b='abc'
3 >>> type(a)
4 <class 'int'>
5 >>> type(b)
6 <class 'str'>

将类类型赋值给变量:

1 >>> a=int
2 >>> a('123')
3 123
4 >>> type(a)
5 <class 'type'>

将函数赋值给变量:

1 >>> def my_func(x):
2 ...     print(x)
3 ...
4 >>> a=my_func
5 >>> a(8)
6 8

将自定义类赋值给:

1 >>> class Person:
2 ...     pass
3 ...
4 >>> a=Person
5 >>> p = a()
6 >>> type(p)
7 <class '__main__.Person'>

。。。

posted @ 2018-06-10 08:53  icekx  阅读(3569)  评论(0编辑  收藏  举报