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'>

。。。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM