python的模塊相當于java中的包
import 包名.模塊名import 包名.模塊名 as 新模塊名forom 包名 import 模塊名forom 包名 import 模塊名 as 新模塊名在python2.7/2.6提供了json模塊,但是python2.5沒有提供,這時候要想使用json模塊就需要try...except...確保導入模塊,類似于java的try ... catch...
舊版本想使用新的版本才有的功能可以使用該方法
例如:
在Python 3.x中,字符串統一為unicode,不需要加前綴 u,而以字節存儲的str則必須加前綴 b。請利用future的unicode_literals在Python 2.7中編寫unicode字符串
from __future__ import unicode_literalss = 'am I an unicode?'print isinstance(s, unicode)pip install [第三方模塊名]安裝成功之后導入即刻import [第三方模塊名]
注意__init__(self,**kw)中的self相當于java的this
輸出:
(Person:bart man)(Student:lisa women 98)Student...do()...(Person:maggie women)當給對象賦值時候
class Student(object): def __init__(self, name, score): self.name = name self.score = score當我們想要修改一個 Student 的 scroe 屬性時,可以這么寫:
s = Student('Bob', 59) s.score = 60 但是也可以這么寫:
s.score = 1000 顯然,直接給屬性賦值無法檢查分數的有效性。
如果利用兩個方法:
class Student(object): def __init__(self, name, score): self.name = name self.__score = score def get_score(self): return self.__score def set_score(self, score): if score < 0 or score > 100: raise ValueError('invalid score') self.__score = score這樣一來,s.set_score(1000) 就會報錯。
這種使用 get/set方法來封裝對一個屬性的訪問在許多面向對象編程的語言中都很常見。
但是寫 s.get_score() 和 s.set_score() 沒有直接寫 s.score 來得直接。
有沒有兩全其美的方法?—-有。
因為Python支持高階函數,在函數式編程中我們介紹了裝飾器函數,可以用裝飾器函數把 get/set 方法“裝飾”成屬性調用:
class Student(object): def __init__(self, name, score): self.name = name self.__score = score @property def score(self): return self.__score @score.setter def score(self, score): if score < 0 or score > 100: raise ValueError('invalid score') self.__score = scores = Student('Bob', 59)s.score = 60print s.scores.score = 1000print s.score注意: 第一個score(self)是get方法,用@property裝飾,第二個score(self, score)是set方法,用@score.setter裝飾,@score.setter是前一個@property裝飾后的副產品
現在,就可以像使用屬性一樣設置score了:
>>> s = Student('Bob', 59)>>> s.score = 60>>> print s.score60>>> s.score = 1000Traceback (most recent call last): ...ValueError: invalid score說明對 score 賦值實際調用的是 set方法。
由于Python是動態語言,任何實例在運行期都可以動態地添加屬性。   如果要限制添加的屬性,例如,Student類只允許添加 name、gender和score 這3個屬性,就可以利用Python的一個特殊的__slots__來實現。
顧名思義,_slots_是指一個類允許的屬性列表:
class Student(object): __slots__ = ('name', 'gender', 'score') def __init__(self, name, gender, score): self.name = name self.gender = gender self.score = score現在,對實例進行操作:
>>> s = Student('Bob', 'male', 59)>>> s.name = 'Tim' # OK>>> s.score = 99 # OK>>> s.grade = 'A'Traceback (most recent call last): ...AttributeError: 'Student' object has no attribute 'grade'__slots__的目的是限制當前類所能擁有的屬性,如果不需要添加任意動態的屬性,使用__slots__也能節省內存。
在Python中,函數其實是一個對象:
>>> f = abs>>> f.__name__'abs'>>> f(-123)123由于 f 可以被調用,所以,f 被稱為可調用對象。
所有的函數都是可調用對象。
一個類實例也可以變成一個可調用對象,只需要實現一個特殊方法call()。
我們把 Person 類變成一個可調用對象:
class Person(object): def __init__(self, name, gender): self.name = name self.gender = gender def __call__(self, friend): print 'My name is %s...' % self.name print 'My friend is %s...' % friend現在可以對 Person 實例直接調用:
>>> p = Person('Bob', 'male')>>> p('Tim')My name is Bob...My friend is Tim...單看 p(‘Tim’) 你無法確定 p 是一個函數還是一個類實例,所以,在Python中,函數也是對象,對象和函數的區別并不顯著。
新聞熱點
疑難解答