读《深入理解py特性》その一
这是确认上岸的第一篇 ブログ, 还没正式收到offer, 还不敢太得意,终于可以稍微放心下, 提升下自己了,, 关于这本书,JD白条,嫖了60块的券, 感谢京东,, 一 断言方法,assert 因为1 等于2, 会抛出AssertionError: fuck it的异常 def test(): assert 1 == 2, “fuck it” print(123) test() 下面就是书上一个很好的例子, 价格异常会抛出异常。 def apply_discount(product, discount): price = int((product[‘price’]) * (1.0 - discount)) assert 0 <= price <= product[‘price’] return price 但是断言需要注意, 元组断言永不言败,手动滑稽 也会抛出异常, 说它永远为真。 def test(): assert (1 == 2, “fuck it”) print(123) test() 123 E:/demo/atest.py:2: SyntaxWarning: assertion is always true, perhaps remove parentheses? assert (1 == 2, “fuck it”) 二 上下文管理器和with 下面是with,