Python系統學習 07

2021-08-20 22:10:28 字數 4452 閱讀 7326

一種規範,寫**時的規範

from abc import abcmeta, abstractmethod

class

istream

(metaclass=abcmeta):

@abstractmethod

defread

(self, data):

print('read super method')

@abstractmethod

defwrite

(self, data):

print('write super method')

class

socketstream

(istream):

defread

(self, data):

print('read sub method')

defwrite

(self, data):

print('write sub method')

p = socketstream()

p.read(1)

python-對於一些相似的方法,不用前置規定,都是約定俗成。

具有共同屬性或方法就互為「鴨子型別」

class

person:

country = 'china'

def__init__

(self, name, age):

self.name = name

self.age = age

p1 = person('morgan', 12) #封裝

# (廣義封裝)封裝到物件中的屬性就是一種封裝

# (狹義封裝)私有封裝

2.1.1私有靜態字段
class

person:

country = 'china'

# 公有靜態變數

__name = 'morgan'

# 私有靜態字段

deffunc

(self):

print(person.__name)

class

people

(person):

deffunc1

(self):

pass

print(person.country)

print(person.__name) #獲取不到這個私有變數值

p1 = person()

p1.__name #獲取不到這個私有變數值

p2 = people()

p2.__name #子類中也不能訪問父類的私有靜態字段

2.1.2.1方法的分類:
class

person:

def__init__

(self, name, age):

self.name = name

self.__age = age

@property

defage

(self):

return self.__age

@age.setter

defage

(self, new_age):

self.__age = new_age

print(new_age)

@age.deleter

defage

(self):

print(777)

p1 = person('morgan', 29)

p1.age = 18

del p1.age #配合上面deleter裝飾器使用

一般是由類名呼叫,有些情況,對於類內部的方法,無需物件直接呼叫,而類名直接呼叫。

class

goods:

__discount = 0.8

def__init__

(self, product, price, discount):

self.product = product

self.__price = price

self.__discount = discount

@property

defprice

(self):

return self.__price*self.__discount

@price.setter

defprice

(self, new_price):

print(self.__price = new_price)

@classmethod

defdiscount

(cls, new_discount):

goods.__discount = new_discount

p1 = goods()

goods.discount(0.85)

print(p1.price)

不需要傳入類名和物件直接呼叫即可。

class

a:def

__init__

(self):

pass

@statiscmethod

deflogin

(username, pwd):

print(username, pwd)

a.login('morgan', 123)

自省

反射:

class

person:

def__init__

(self, name, age):

self.name = name

self.age = age

deftest

(self, new_discount):

print(new_discount)

defage(self):

print(self.age)

p = person('morgan', 19)

print(getattr(p, 'age')) #輸出:19

在python中一切皆物件,凡是通過。

類的裝飾器—回去看

反射應用場景

_len_

_call_

_item_

_new_

class

foo:

def__init__

(self,name):

self.name=name

def__getitem__

(self, item):

print(self.__dict__[item])

def__setitem__

(self, key, value):

self.__dict__[key]=value

def__delitem__

(self, key):

print('del obj[key]時,我執行')

self.__dict__.pop(key)

def__delattr__

(self, item):

print('del obj.key時,我執行')

self.__dict__.pop(item)

f1=foo('sb')

f1['age']=18

f1['age1']=19

f1['name']='morgan'

print(f1.__dict__) #輸出:

設計模式—單例設計模式

class

a: __instance = none

def__new__

(cls, *args, **kwargs)

ifcls.__instance

isnone:

obj = object.__new__(cls)

cls.__instance = obj

return cls.__instance

a = a()

b = a()

print(a, b) #a,b位址一樣

Python系統學習 02

資料型別 數值用於計算,布林值用於條件執行等等。例子 s i m a student 錯誤,也可以在 前面新增轉義字元 s i m a student s1 morgan s2 xu print s1 s2 morgan xu s1 morgan print s1 8 morgan morgan m...

python 系統學習筆記(四) list

列表是一種用於儲存有序元素集合的資料結構,即你可以在列表中儲存元素序列。考慮乙個購物清單,上面有你需要購買的物品列表,只不過你可能希望以分號分隔他們而到python變成了逗號。列表元素被包含在方括號中,這樣python就會才會明白你指定的是乙個列表。一旦列表建立完畢,我們可以對其元素進行新增,刪除和...

python 系統學習筆記(十一) sys

sys.startswith 是用來判斷乙個物件是以什麼開頭的,比如在python命令列輸入 abc startswith ab 就會返回true 問題 實現命令列讀引數讀檔案 python readfile.py c test.txt d test.txt python readfile.py h...