9.16 args和*kwargs的强制参数签名
对任何涉及到操作函数调用签名的问题,你都应该使用 模块中的签名特性。我们最主要关注两个类:Signature
和 Parameter
。下面是一个创建函数前面的交互例子:
一旦你有了一个签名对象,你就可以使用它的 bind()
方法很容易的将它绑定到 和 *
kwargs
上去。下面是一个简单的演示:
- >>> def func(*args, **kwargs):
- ... bound_values = sig.bind(*args, **kwargs)
- ... for name, value in bound_values.arguments.items():
- ... print(name,value)
- ...
- >>> # Try various examples
- >>> func(1, 2, z=3)
- x 1
- y 2
- z 3
- >>> func(1)
- x 1
- >>> func(1, z=3)
- x 1
- z 3
- >>> func(y=2, x=1)
- y 2
- >>> func(1, 2, 3, 4)
- Traceback (most recent call last):
- ...
- File "/usr/local/lib/python3.3/inspect.py", line 1972, in _bind
- raise TypeError('too many positional arguments')
- TypeError: too many positional arguments
- >>> func(y=2)
- Traceback (most recent call last):
- ...
- File "/usr/local/lib/python3.3/inspect.py", line 1961, in _bind
- raise TypeError(msg) from None
- TypeError: 'x' parameter lacking default value
- >>> func(1, y=2, x=3)
- Traceback (most recent call last):
- ...
- File "/usr/local/lib/python3.3/inspect.py", line 1985, in _bind
- '{arg!r}'.format(arg=param.name))
- TypeError: multiple values for argument 'x'
下面是一个强制函数签名更具体的例子。在代码中,我们在基类中先定义了一个非常通用的 init()
方法,然后我们强制所有的子类必须提供一个特定的参数签名。
下面是使用这个 类的示例:
- >>> import inspect
- >>> print(inspect.signature(Stock))
- (name, shares, price)
- >>> s1 = Stock('ACME', 100, 490.1)
- >>> s2 = Stock('ACME', 100)
- Traceback (most recent call last):
- ...
- TypeError: 'price' parameter lacking default value
- >>> s3 = Stock('ACME', 100, 490.1, shares=50)
- Traceback (most recent call last):
- ...
- TypeError: multiple values for argument 'shares'
- >>>
在最后的一个方案实例中,我们还可以通过使用自定义元类来创建签名对象。下面演示怎样来实现:
当我们自定义签名的时候,将签名存储在特定的属性 中通常是很有用的。这样的话,在使用 inspect
模块执行内省的代码就能发现签名并将它作为调用约定。
- >>> import inspect
- >>> print(inspect.signature(Stock))
- (name, shares, price)
- >>> print(inspect.signature(Point))
- (x, y)
- >>>