Python Modules 大小事
File Import
- 任一 .py附屬檔名 的python原始碼檔案都是一個module。
- 在同一 session (process)裏,相同的module只會實際被 import一次。
- Simple Import Statements
(1) import myfile
myfile.funcCall()(2) from myfile import myAttr
print myAttr(3) from myFile import * # 複製所有的變數
(4) delete myModule # just like unload the specified moudle - Python Module 路徑搜尋順序
(1) Top-Level File的所在目錄
(2) PYTHONPATH 定義的目錄
(3) Standard Library 的目錄
(4) .pth 檔案內的內容
.pth (即為 path之意) 為 Python特有之檔案格式。其內存有使用者定義的額外搜尋目錄。
Package Import
- Import 也可以直接指命目錄名稱,某目錄下意同某一 Package。如:
import myDir # import 『myDir』 這個 package
import myDir2.myModule - 如要 import package, 則要 import 的目錄內需含有 __init__.py 檔。否則 import 將會失敗。
Android, iPhone 及 Symbian OS 開發環境之比較
|
|
Android |
iPhone |
Nokia |
|
|
作業系統 |
Android 1.5 Android 1.6 (Linux Kernel Based) |
iPhone OS |
Series 40 Platform
|
S60 Platform
|
|
開發工具 |
Eclipse |
Xcode |
NetBeans / Eclipse |
Carbide.c++(Eclipse) |
|
Language |
Java |
Objective-C |
J2ME |
Symbian C++ Java Python |
Python String (字串) 基礎
字串表示式:
Python提供兩種字串表式式 – 雙引號/單引號。例如: 『這是一個雙引號字串』 , ‘這是一個單引號字串’
然而對於Python語言而言,使用 單引號/雙引號 對於內部運作並無不同。其最主要目的為「讓你在字串表示中能更方便的使用引號」。
例如: ‘這個字串裏面請特別注意 『這裏』 ‘
整數的字串格式化
str(my_int) or repr(my_int)
長字串的使用
如果有跨行的字串則可於字串前後使用連續三個單引號來包含字串內容。例如:
』’這是第一行
這是第二行
這是第三行』’
另外因字串前後已有三個引號,故在長字串內的引號(不論單/雙)皆不需要再使用跳脫字元。例如:
』’這是第一行
這是第二行,但是不用』跳』囉
這是第三行』’
Raw String (myString = r’This is a raw string’)
Raw string最主要目的為「使字串用的跳脫字元失效」,也就是如,字串內的\n等,不再定義為它原本的換行功能。幾個範例如下:
>>>print r’String line one, \nString Line2′
String line one, \nString Line2>>>print r’Hello, Who\’s site here?』
Hello, Who\’s site here?跳脫字元的失效有一個唯一例外,就是最後一個字元的位置時。
>>>print r’Hello, this is invalid statement\’
為什麼? 因這樣的語法將使得直譯器搞不清楚字串是哪裡結束阿!?
Unicode String
>>>print u’this is the unicode string’
this is the unicode string
字串的連結
>>>print ‘the part01 ‘ + ‘ concated with this one’
the part01 concated with this one
字串格式化
>>>stmt = 『%s, this is a %s sample』
>>>values= (‘Part01′, ‘Python String’)
>>>print stmt % values
Part01, this is a Python String sample