atribute of object in python?

是否正确更改对象的__name__属性的值,如下例所示:

>>>
>>> def f(): pass
...
>>> f.__name__
'f'
>>> b = f
>>> b.__name__
'f'
>>> b.__name__ = 'b'
>>> b
<function b at 0x0000000002379278>
>>> b.__name__
'b'
>>>

Changing a function's name doesn't make the new name callable:

>>> def f(): print 'called %s'%(f.__name__)
...
>>> f()
called f
>>> f.__name__ = 'b'
>>> f()
called b
>>> b()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined

You'd have to define b in order to call it. And as you saw, simply assigning b=f doesn't define a new function.

链接地址: http://www.djcxy.com/p/7074.html

上一篇: 避免在可选使用位置的情况下过滤Android电子市场

下一篇: Python中对象的属性?