相关疑难解决方法(0)

Python如何区分内建函数中显式传递的None作为参数

我尝试了下一个代码:

>>> f = object()

# It's obvious behavior:
>>> f.foo
Traceback (most recent call last):       
  File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'foo'

# However, the next one is surprising me!
>>> getattr(f, 'foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'foo'

# And this one returns None as expected:
>>> getattr(f, 'foo', None)
Run Code Online (Sandbox Code Playgroud)

然后我getattr()在PyCharm IDE中找到了这个伪签名:

def getattr(object, name, default=None): # known …
Run Code Online (Sandbox Code Playgroud)

python built-in python-internals

3
推荐指数
1
解决办法
320
查看次数

标签 统计

built-in ×1

python ×1

python-internals ×1