Python的一等公民
Python 中的一等公民,都具有以下特性:
- 可以赋值给一个变量
- 可以添加到集合对象中
- 可以作为参数传递给函数
- 可以当做函数的返回值
针对赋值给变量及添加到集合对象中,代码予以展示:
def func():
print 'the func function is executed'
class Test:
def __init__(self):
print 'An instance of Test class is initialized'
obj_list = []
obj_list.append(func)
obj_list.append(Test)
for item in obj_list: # 添加至集合对象中
print(item())
那么上述例子,代码运行结果如下:
the func function is executed # 函数func被执行
None # 函数func因没有return,返回None
An instance of Test class is initialized # 类Test被实例化,因此类Test的__init__(self)函数被调用,因而打印出 An instance of Test class is initialized
<__main__.Test object at 0x0000024AB34526A0> # 类Test被实例化出一个类 Test 对象,由于类 Test 没有重写__str__方法,因此直接打印出了这个 Test 对象的内存地址
Reference
FEATURED TAGS
algorithm
algorithmproblem
architecturalpattern
architecture
aws
c#
cachesystem
codis
compile
concurrentcontrol
database
dataformat
datastructure
debug
design
designpattern
distributedsystem
django
docker
domain
engineering
freebsd
git
golang
grafana
hackintosh
hadoop
hardware
hexo
http
hugo
ios
iot
java
javaee
javascript
kafka
kubernetes
linux
linuxcommand
linuxio
lock
macos
markdown
microservices
mysql
nas
network
networkprogramming
nginx
node.js
npm
oop
openwrt
operatingsystem
padavan
performance
programming
prometheus
protobuf
python
redis
router
security
shell
software testing
spring
sql
systemdesign
truenas
ubuntu
vmware
vpn
windows
wmware
wordpress
xml
zookeeper