forked from misakar/pythonCookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.11.2.py
37 lines (26 loc) · 942 Bytes
/
8.11.2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
# encoding: utf-8
"""
8.11.2.py
~~~~~~~~~
添加关键字参数
"""
class Structure:
_fields = [] # 初始化变量名称列表
def __init__(self, *args, **kwargs):
if len(*args) > len(self._fields):
raise TypeError('Expect {} arguments'.format(len(self._fields)))
# 处理位置参数
for name, value in zip(self._fields, *args):
setattr(self, name, value)
# 处理关键字参数
for name in self._fields:
setattr(self, name, kwargs.pop(name)) # 使用pop出栈操作,避免字典中的值重复
# 处理多余参数
if kwargs:
# ','.join(dict): 返回字典键组成的字符串
raise TypeError('Invalid argument(s) {}'.format(','.join(kwargs)))
# 示例使用
if __name__ == '__main__':
class Stock(Structure):
_fields = ['name', 'shares', 'price']