包含标签 python 的文章

Python 枚举使用

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 from enum import Enum class Gender(Enum): male = 0 female = 1 print(Gender(0)) # Gender.……

阅读全文

Mac 安装 mysqlclient

Python 版本 1 2 (venv) ➜ python --version Python 3.8.2 安装 1 2 3 4 5 brew install mysql export LDFLAGS="-L/usr/local/opt/openssl/lib" export CPPFLAGS="-I/usr/local/opt/openssl/include" pip install mysqlclient brew uninstall mysql ……

阅读全文

Python 中的else

if…else 最常见的 else 1 2 3 4 if 1 > 0: pass else: pass for else for 循环中 只有 for 循环结束了才执行, 注意空循环也会执行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 for i in range(3): print(i) else: print("end") # 0 # 1 # 2 # end for i in range(3): print(i) if i == 1: break else: print("end") # 0 # 1 try… else 这个就很好理解了, else 只会在 try 未发生任何异常的时候执行 finally 在所有状态下都会执行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 try: 1 except Exception as e: print(e) else: print("else") finally: print("finally") # else # finally try: 1/0 except Exception as e: print(e) else: print("else") finally: print("finally") # division by zero # finally ……

阅读全文

python 异常处理

异常处理 1 2 3 4 5 def demo(): try: 1 / 0 except Exception as e: print(type(e)) #<class 'ZeroDivisionError'> 自定义异常 1 2 class CustomError(Exception): pass 异常链 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class CustomError(Exception): pass def demo(): try: 1 / 0 except Exception as e: print(type(e)) #<class 'ZeroDivisionError'> raise CustomError() from e <class 'ZeroDivisionError'> Traceback (most recent call last): File "/Users/tiger/work/customer/dd.……

阅读全文