【发布时间】:2011-06-13 11:55:40
【问题描述】:
如何在 python 中进行安全转换
在 c# 中,我可以安全地将关键字转换为 as,例如:
string word="15";
var x=word as int32// here I get 15
string word="fifteen";
var x=word as int32// here I get null
python有类似的东西吗?
【问题讨论】:
-
请注意,Python 实际上并没有 转换;这只是将值传递给其他类型的构造函数。
-
使用 C# 进行 C# 编程。在 Python 中,您希望使用异常,而不是空值。
-
这在 C# 中甚至无效。这是一个编译错误。