col*_*nta 7 python metaprogramming
免责声明这只是元编程的练习,没有实际意义.
我分配__getitem__和__getattr__函数对象的方法,但没有效果...
def foo():
print "foo!"
foo.__getitem__ = lambda name: name
foo.__getattr__ = lambda name: name
foo.baz = 'baz'
Run Code Online (Sandbox Code Playgroud)
我们可以为函数指定属性的完整性检查:
>>> foo.baz
'baz'
Run Code Online (Sandbox Code Playgroud)
整齐."神奇的吸气者"怎么样?
>>> foo.bar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'bar'
>>> foo['foo']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'function' object is not subscriptable
>>> getattr(foo, 'bar')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'bar'
Run Code Online (Sandbox Code Playgroud)
是否有可能在函数对象上有一个"魔术吸气剂"?
不!分配__getitem__给实例不适用于任何类型的对象:
>>> class A(object):
... pass
...
>>> a = A()
>>> a.__getattr__ = lambda name: name
>>> a.foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'foo'
Run Code Online (Sandbox Code Playgroud)
而且你无法__getattr__在内置函数类型上定义:
>>> import types
>>> types.FunctionType.__getitem__ = lambda name: name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'function'
Run Code Online (Sandbox Code Playgroud)
而且你不能子类types.FunctionType:
>>> import types
>>> class F(types.FunctionType):
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
type 'function' is not an acceptable base type
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3663 次 |
| 最近记录: |