如何从AttributeError Python提取属性名称
我将从AttributeError提取名称。 我有:
x = 10
try:
x.append(20)
except AttributeError as e:
print(f"name of Atrribute is: {e}")
结果:
name of Atrribute is: 'int' object has no attribute 'append'
我需要“附加”,谢谢!
i have would to extract name from AttributeError.
i have:
x = 10
try:
x.append(20)
except AttributeError as e:
print(f"name of Atrribute is: {e}")
And result:
name of Atrribute is: 'int' object has no attribute 'append'
I need 'append', Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于
python> = 3.10
,attributeError
的实例具有name
obj obj attribute:但是< /strong>如果您手动
提高attributeError
,则其name
和obj
将为none
,以防万一您要提高
attributeError
自己,您可以设置name
和obj
手动,例如For
python >= 3.10
, instances ofAttributeError
have aname
andobj
attribute:But if you manually
raise AttributeError
, itsname
andobj
will beNone
In case you want to raise an
AttributeError
yourself, you can set thename
andobj
manually, e.g.您可以在空间上将消息拆分并取最后一个元素。然后从此元素中剥离
'
。输出为
trribute的名称为:Append
。请注意,如果Python决定在以后的版本中传达更改的消息,这可能会破裂。
You can split the message at the spaces and take the last element. Then strip the
'
from this element.The output is
name of Atrribute is: append
.Be aware that this might break if Python decides to deliver a changed message in future versions.