關於我
一個有思想的程序猿,終身學習實踐者,目前在一個創業團隊任team lead,技術棧涉及Android、Python、Java和Go,這個也是我們團隊的主要技術棧。
Github:https://github.com/hylinux1024
微信公眾號:終身開發者(angrycode)
在Python
中一切都是對象。如果要在Python
中表示一個對象,除了定義class
外還有哪些方式呢?我們今天就來盤點一下。
0x00 dict
字典或映射存儲KV
鍵值對,它對查找、插入和刪除操作都有比較高效率。用一個dict
對象可以非常容易的表示一個對象。dict
的使用也很靈活,可以修改、添加或刪除屬性。
>>> student={
'name':'jack',
'age':18,
'height':170
}
>>> student
{'name': 'jack', 'age': 18, 'height': 170}
# 查看屬性
>>> student['name']
'jack'
# 添加屬性
>>> student['score']=89.0
>>> student
{'name': 'jack', 'age': 18, 'height': 170, 'score': 89.0}
# 刪除屬性
>>> del student['height']
>>> student
{'name': 'jack', 'age': 18, 'score': 89.0}
0x01 tuple
tuple
也可以表示一個對象,相對於dict
來說,它是不可變的,一旦創建就不能隨意修改。tuple
也只能通過下標來訪問對象的屬性,因此當屬性比較多時使用起來沒有dict
方便。
# 對象屬性為name、age、height
>>> student=('jack',18,170.0)
>>> student
('jack', 18, 170.0)
>>> student[1]
18
# tuple不能修改
>>> student[2]=175.0
TypeError: 'tuple' object does not support item assignment
0x02 collections.namedtuple
顧名思義namedtuple
就是命名元組。它是tuple
數據類型的擴展,同樣地一旦創建,它的元素也是不可變的。與普通元組相比命名元組可以通過“屬性名”來訪問元素。
>>> from collections import namedtuple
>>> Point = namedtuple('Point','x,y,z')
>>> p = Point(1,3,5)
>>> p
Point(x=1, y=3, z=5)
>>> Point = namedtuple('Point','x y z')
>>> p = Point(1,3,5)
>>> p
Point(x=1, y=3, z=5)
>>> p.x
1
>>> p.y = 3.5
AttributeError: can't set attribute
# 可以看出通過namedtuple定義對象,就是一個class類型的
>>> type(p)
<class '__main__.Point'>
對於一個簡單的對象,我們使用namedtuple
很方便的來定義,它比定義一個普通class
要有更好的空間性能。
0x03 type.NamedTuple
Python3.6
中新增了type.NamedTuple
類,它與collections.namedtuple
的操作是類似的。不過,要定義NamedTuple
就稍微不一樣了。
>>> from typing import NamedTuple
# 定義Car類,繼承於NamedTuple,並定義屬性color、speed、autmatic
>>> class Car(NamedTuple):
color:str
speed:float
automatic:bool
>>> car = Car('red',120.0,True)
>>> car
Car(color='red', speed=120.0, automatic=True)
>>> type(car)
<class '__main__.Car'>
# tuple都是不可變的
>>> car.speed = 130.0
AttributeError: can't set attribute
0x04 types.SimpleNamespace
使用SimpleNamespace
也可以很方便的定義對象。它的定義等價於
class SimpleNamespace:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def __repr__(self):
keys = sorted(self.__dict__)
items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
return "{}({})".format(type(self).__name__, ", ".join(items))
def __eq__(self, other):
return self.__dict__ == other.__dict__
例如定義一個Car
對象
>>> car = SimpleNamespace(color='blue',speed=150.5,automatic=True)
>>> car
namespace(automatic=True, color='blue', speed=150.5)
>>> car.color
'blue'
>>> car.speed = 120
>>> car
namespace(automatic=True, color='blue', speed=120)
# 動態添加屬性
>>> car.shift = 23
>>> car
namespace(automatic=True, color='blue', shift=23, speed=120)
# 刪除屬性
>>> del car.shift
>>> car
namespace(automatic=True, color='blue', speed=120)
0x05 struct.Struct
這是一個結構體對象,可以把C
語言中的struct
序列化成Python
對象。例如處理文件中的二進制數據或從網絡中請求的數據,可以使用這個struct.Struct
來表示。
使用struct
好處是數據格式是預先定義好的,可以對數據進行打包成二進制數據,空間效率會好很多。
# 定義一個struct,'1sif'表示數據的格式,1s一個字符長度,i表示整數,f表示浮點數
>>> Student=Struct('1sif')
# 使用pack方法打包數據,存儲性別、年齡、身高
>>> stu = Student.pack(b'm',18,175.0)
>>> stu
b'm\x00\x00\x00\x12\x00\x00\x00\x00\x00/C'
# unpack方法解包
>>> Student.unpack(stu)
(b'm', 18, 175.0)
0x06 class
class
當然是定義一個對象的標准方式了。在Python
定義類也非常簡單,除了可以定義屬性還可以定義方法。
>>> class Student:
def __init__(self,name,age,height):
self.name = name
self.age = age
self.height = height
def printAge(self):
print(self.age)
>>> stu = Student('jack',18,175.0)
# 如果想讓定義的對象輸出屬性信息可以重寫__repr__方法
>>> stu
<__main__.Student object at 0x10afcd9b0>
>>> stu.name
'jack'
>>> stu.age = 19
0x07 總結一下
本文盤點Python
中定義對象各種的方法,除了class
,還有有dict
、tuple
、namedtuple
、NamedTuple
、SimpleNamespace
和Struct
。
如果一個對象屬性不多可以使用tuple
;
如果一個對象屬性不可變可以考慮使用namedtuple
或NamedTuple
;
如果一個對象要轉成JSON
進行傳輸可以使用dict
;
如果考慮比較空間性能,可以使用Struct
。
0x08 學習資料
- Python Tricks: A Buffet of Awesome Python Features
——Dan Bader