Bạn không thể sử dụng việc thực hiện tương tự như đối tượng kết quả của os.stat() và những người khác. Tuy nhiên Python 2.6 có một chức năng nhà máy mới tạo ra một kiểu dữ liệu tương tự được gọi là tuple có tên. Một tuple có tên là một tuple có các slot cũng có thể được giải quyết theo tên. Các tuple được đặt tên không nên yêu cầu bất kỳ bộ nhớ nhiều hơn, theo tài liệu, hơn một tuple thường xuyên, vì họ không có một từ điển mỗi trường hợp. Chữ ký chức năng nhà máy là:
collections.namedtuple(typename, field_names[, verbose])
Đối số đầu tiên chỉ định tên của các loại mới, số thứ hai là một chuỗi (không gian hoặc dấu phẩy tách ra) chứa các tên trường và cuối cùng, nếu verbose là đúng, chức năng nhà máy cũng sẽ in lớp được tạo ra.
Ví dụ
Giả sử bạn có một tuple chứa một tên người dùng và mật khẩu.Để truy cập vào tên người dùng bạn có được những item ở vị trí zero và mật khẩu được truy cập ở vị trí một:
credential = ('joeuser', 'secret123')
print 'Username:', credential[0]
print 'Password:', credential[1]
Không có gì sai với mã này là nhưng tuple không phải là tự tài liệu. Bạn phải tìm và đọc tài liệu về vị trí của các trường trong tuple. Đây là nơi tuple được đặt tên có thể đến để giải cứu. Chúng ta có thể mã hóa lại các ví dụ trước như sau:
import collections
# Create a new sub-tuple named Credential
Credential = collections.namedtuple('Credential', 'username, password')
credential = Credential(username='joeuser', password='secret123')
print 'Username:', credential.username
print 'Password:', credential.password
Nếu bạn quan tâm về những gì mã trông giống như cho mới được tạo ra Credential kiểu bạn có thể thêm verbose = Đúng như danh sách đối số khi tạo kiểu, trong này trường hợp cụ thể chúng tôi nhận được kết quả như sau:
import collections
Credential = collections.namedtuple('Credential', 'username, password', verbose=True)
class Credential(tuple):
'Credential(username, password)'
__slots__ =()
_fields = ('username', 'password')
def __new__(_cls, username, password):
return _tuple.__new__(_cls, (username, password))
@classmethod
def _make(cls, iterable, new=tuple.__new__, len=len):
'Make a new Credential object from a sequence or iterable'
result = new(cls, iterable)
if len(result) != 2:
raise TypeError('Expected 2 arguments, got %d' % len(result))
return result
def __repr__(self):
return 'Credential(username=%r, password=%r)' % self
def _asdict(t):
'Return a new dict which maps field names to their values'
return {'username': t[0], 'password': t[1]}
def _replace(_self, **kwds):
'Return a new Credential object replacing specified fields with new values'
result = _self._make(map(kwds.pop, ('username', 'password'), _self))
if kwds:
raise ValueError('Got unexpected field names: %r' % kwds.keys())
return result
def __getnewargs__(self):
return tuple(self)
username = _property(_itemgetter(0))
password = _property(_itemgetter(1))
các tên tuple không chỉ cung cấp quyền truy cập vào các lĩnh vực theo tên mà còn chứa các chức năng helper như _make() chức năng mà giúp tạo ra một thể hiện ủy nhiệm từ một chuỗi hoặc có thể lặp lại. Ví dụ:
cred_tuple = ('joeuser', 'secret123')
credential = Credential._make(cred_tuple)
Các tài liệu thư viện python cho namedtuple có thêm thông tin và mã ví dụ, vì vậy tôi đề nghị bạn nên take a peek.