我想检查变量是否存在.现在我正在做这样的事情:
try:
myVar
except NameError:
# Do something.
Run Code Online (Sandbox Code Playgroud)
还有其他方法没有例外吗?
为了避免我可以用Google搜索的所有标准答案,我将提供一个你可以随意攻击的例子.
C#和Java(和太多的人)有很多类型的一些"溢出"的行为,我不都不像(如type.MaxValue + type.SmallestValue == type.MinValue例如: int.MaxValue + 1 == int.MinValue).
但是,看到我的恶性,我会通过扩展这种行为来增加对这种伤害的一些侮辱,让我们说一个Overridden DateTime类型.(我知道DateTime密封在.NET中,但是为了这个例子,我使用的是一种与C#完全相同的伪语言,除了DateTime没有密封的事实).
被覆盖的Add方法:
/// <summary>
/// Increments this date with a timespan, but loops when
/// the maximum value for datetime is exceeded.
/// </summary>
/// <param name="ts">The timespan to (try to) add</param>
/// <returns>The Date, incremented with the given timespan.
/// If DateTime.MaxValue is exceeded, the sum wil 'overflow' and
/// continue from DateTime.MinValue.
/// </returns>
public DateTime override Add(TimeSpan …Run Code Online (Sandbox Code Playgroud) 我已经深入了解(至少在PHP中)使用try... catch块进行流量控制是badbadmojo .我学到的只是使用它们来处理意外错误,而不是确定程序的逻辑流程,因为catch块很昂贵.
现在我正在学习python,我看到了很多例外和EAFP原则.这是否意味着python在处理异常方面更有效率,所以我不需要为流量控制担心它们,或者原理是否仍然存在?如果没有,那么PHP是规范的异常(与其他语言相比),还是Python?