--- title: Trick in Python description: Showcase for all cool tricks in Python. tags: python,tricks,cool GA: UA-206161787-1 lang: zh --- # Tricks in Python ## Load Python file (`.py`) into Interpreter 在執行Python Interpreter時,加入`-i`的command line flag,我們可以載入整個Python檔案到直譯器裡,就彷彿我們一行一行的把檔案的內容輸入直譯器中。在一些需要人機互動的程式測試時非常好用,像是在寫網頁爬蟲或是機器人時,可能寫到一半我們會想要測試這些程式碼是如何與網頁互動,進而除錯或是改善流程。 ```python= # scraper.py def enter_username(): username_input = dom.get_element_by_id("username_input") username_input.input("username1") ``` ```bash= $ python3 -i scraper.py >>> enter_username() # Observe how the function interacts with the webpage. ```