3.3b Use Classes - Text Send

寫 Code 發簡訊

Twilio

  • 並非來自 Python Standard Library,是成千上萬的 Python Packages which are available to download to use

  • 安裝方式

  • 要先成功安裝pip,twilio 才能安裝

  • pip是什麼?

  • pip 的Guideline

  • 安裝時輸入的sudo是?

  • 詳細解說

  • 要使用sudo - H的 command line

  • Github

  • 解說 Class其實就像blueprint 可以創造不同的 object

  • 比較 turtle 和 twilio

  • turtle 是個 file,內有 Turtle() 和 Screen() 這些 class

  • rest 是個 folder,est 內的 python 檔案有個class TwilioRentClient,也先call init function,創造記憶體空間

  • 都先跑 init 創造記憶體空間

關係圖

twilio 這個檔案夾下是 rest 檔案夾,rest 內的 python 檔案有個class TwilioRentClient,實際上在 call init function

from...import

let you import specific attributes from a module

from a file(module) name import class

send_sms.py

  • 註冊後到 Dashboard 的 API Credential 查看 SID 和 Token,再套到 Code 裡面

Anaconda

  • Python 的大補帖,方便把重要套件都一次裝齊全

  • 學習資源

  • Spyder, Anaconda's alternative to IDLE

  • 3.3c Use Classes - Profanity Editor

案例:檢查文件中有沒有不雅的字眼

流程

  1. 打開檔案

  2. 先用到 open(),打開檔案

  3. 打開的檔案要讀取,用read method讀檔案

  4. 讀完關掉,close()

  5. 把檔案文字丟到網站

  6. 網站回傳結果

  7. 判斷結果

urllib

  • 屬於 python standard 原本就有的 module

  • urlopen 是 function inside

為何 urlopen() 和 open() 都沒有要建一個 class?

只是表面看不出來,其實 some classes init

  • 3.4a Make Classes - Movie Website

Google Python Style Guide

  • Providing conventions how write python from the world

  • Google 建議建立新的 class 時,class 的名字第一個字要大寫

  • e.g. class Movie

繼承別的檔案的 Class

  • 在一個檔案創建的 class,可以在別的檔案使用 import 檔名(不用加py)

  • 在新檔案創建 object

  • some module or file name dot a class name

e.g. toy_story = media.Movie()

三個觀念

在這邊我們用了新建的檔案1( media.py )裏面有個 class Movie,在檔案2寫了 toy_story = media.Movie(),寫這一行的背後是,call the init function, define inside class movie ; 這個 init 也叫 constructor 因為 constructs space and memory for the new instance

Defining _init_

  • underscores 告訴 programmers,essentially reserved in Python,this is special method or function

  • 是一種慣例(convention)

  • 詳細解釋

Method 跟 Function 的不同

解釋什麼是 Method, A function which a member of class

Self

  • self as itself or the instance being created

  • self is the object being created

  • Udacity 解釋

  • 目前了解就像這個例子裡創造的 object , toy_story, self 用在你define class 內

你就可以開始利用 self 創造這個 function 下的 property

instance variables(e.g. title, storyline...) 和 instance methods(e.g. def _init_) 其實就是 define class 時寫的值

results matching ""

    No results matching ""