Python 筆記

Python notes

R4 Cheng
1 min readNov 22, 2020

有新知識就更新,有錯誤歡迎留言指正,感恩

Content:

+ Module vs. Package

+ Clean code 必學技巧

+ Lambda

+ Bad pattern to avoid

Module vs. Package

A module is a .py file, a package is a folder with an __init__.py file. When you import a package as a module, the contents of __init__.py module are imported.

A Python module is simply a Python source file, which can expose classes, functions and global variables.

  • module = file
  • package = directory

Clean code 必學技巧

5 coolest Python features to learn

  • list comprehensions
  • generator expression
  • slice assignment
  • iterable unpacking (destructure)
  • dictionary comprehensions

list init

list = [False] * len(3) # [False, False, False]

Lambda

<空>

Bad pattern (要避免的 pattern)

ITM (Initialize Then Modify)

  • 先宣告後修改
// Initialize
i = 0
for t in X:
// Then Modify
i += 1
  • 上例解法: e.g. 改用 enumerate

Reference

[2] Corey schafer. Python OOP Tutorial 1: Classes and Instances. (2016)

--

--

R4 Cheng
R4 Cheng

Written by R4 Cheng

「0」が過去で「1」が未来「今」は何処にもない

No responses yet