Python 特性入門
===
###### tags: `python`
## Abstract

為了方便各位能trace code並且寫出自己的code.
我們必須先對python的特殊語法熟悉一下.
### 0x0. Slice 要怎麼使用舉個例子
### 0x1. list comprehension ? 舉例說明
```python
squares = [x**2 for x in range(10)]
print squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
```
### 0x2. range() 跟 xrange()差在哪? 舉例說明
### 0x3. with 用在哪? 用了有甚麼好處? 舉例說明(open file?)
### 0x4. lambda? 是甚麼? 舉例用法
### 0x5. map, filter and reduce 舉例用法
### 0x6. `*args` 和 `**kwargs` 幹嘛用的?
### 0x7. 甚麼是生成器? 有甚麼優點?
https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00138681965108490cb4c13182e472f8d87830f13be6e88000
### 0x8. yield 怎用? 試用他寫個費氏數
* yield 是個進階版的return。
```python=
def fibonacci_generator():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
# 创建一个斐波那契数列生成器对象
fib_gen = fibonacci_generator()
# 打印前10个斐波那契数
for i in range(10):
print(next(fib_gen))
```
### 0x9. (for debug) isinstance, type 如何用?
```python=
import threading, time, logging, fastcounter
logging.basicConfig(level=logging.DEBUG,
format='(%(threadName)-9s) %(message)s',)
counter = fastcounter.Counter()
def job(num, e):
global counter
counter.increment()
# to-do : logging the hall
e.wait()
# to-do: playing the game
if __name__ == '__main__':
maxconnections = 5
e = threading.Event()
threads = []
for i in range(maxconnections):
threads.append(threading.Thread(name=str(i)+'-th thread' ,target=job, args=(i, e)))
threads[i].start()
while counter.value < maxconnections:
pass
e.set()
for i in range(5):
threads[i].join()
logging.debug("Done.")
```
### 0x10. copy() vs deepcopy()
* 当你需要复制一个列表(或其他可变对象)而不影响原始列表时,可以使用 Python 中的 copy() 方法。这个方法创建一个浅拷贝,也就是复制了原始列表的引用,但不复制其内部的嵌套对象。:
```python3=
original_list = [1, 2, 3, [4, 5]]
# 使用 copy() 方法创建一个新的列表
new_list = original_list.copy()
# 修改新列表中的元素
new_list[0] = 10
new_list[3][0] = 40
# 打印结果
print("原始列表:", original_list)
print("新列表:", new_list)
#原始列表: [1, 2, 3, [40, 5]]
#新列表: [10, 2, 3, [40, 5]]
```
## 安全問題
* [Exploiting Eval(), Exec(), input](https://www.stackhawk.com/blog/command-injection-python/)
* [Exploiting Python Code Injection in Web Applications](https://sethsec.blogspot.com/2016/11/exploiting-python-code-injection-in-web.html)
## 編碼相關
### [Windows 系統上 Python 的文字輸出編碼](https://hackmd.io/@meebox/rJXWIi5p_#Windows-%E7%B3%BB%E7%B5%B1%E4%B8%8A-Python-%E7%9A%84%E6%96%87%E5%AD%97%E8%BC%B8%E5%87%BA%E7%B7%A8%E7%A2%BC)
```python=
# print_encoding.py
import sys
import locale
print('locale.getpreferredencoding():\t{}'.format(
locale.getpreferredencoding())
)
print('sys.getfilesystemencoding():\t{}'.format(
sys.getfilesystemencoding())
)
print('sys.getdefaultencoding():\t{}'.format(
sys.getdefaultencoding())
)
print('sys.stduot.encoding:\t\t{}'.format(
sys.stdout.encoding)
)
```
## misc
### iterators vs generators
https://foofish.net/iterators-vs-generators.html
### virtualenv
virtualenv 可以隔離許多python環境, 方便各種python應用.
### 有没有简单一点的Python小例子小项目?
https://www.zhihu.com/question/51920544/answer/128132480
### Python有GIL为什么还需要线程同步?
https://www.zhihu.com/question/23030421/answer/36220770
### Python暴力猜解Web应用
https://zhuanlan.zhihu.com/p/23394446