Đang chuẩn bị liên kết để tải về tài liệu:
PROGRAMMING IN PYTHON 3 - PART 3

Không đóng trình duyệt đến khi xuất hiện nút TẢI XUỐNG

Tham khảo tài liệu 'programming in python 3 - part 3', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 112 Chapter 3. Collection Data Types total 0 for sale in sales total sale.quantity sale.price print Total 0 .2f .format total prints Total 42.46 The clarity and convenience that named tuples provide are often useful. For example here is the aircraft example from the previous subsection 110 o done the nice way Aircraft collections.namedtuple Aircraft . manufacturer model seating Seating collections.namedtuple Seating minimum maximum aircraft Aircraft Airbus A320-200 Seating 100 220 aircraft.seating.maximum 220 When it comes to extracting named tuple items for use in strings there are three main approaches we can take. print 0 1 .format aircraft.manufacturer aircraft.model Airbus A320-200 Here we have accessed each of the tuple s items that we are interested in using named tuple attribute access. This gives us the shortest and simplest format string. And in Python 3.1 we could reduce this format string to just . But this approach means that we must look at the arguments passed to str.format to see what the replacement texts will be. This seems less clear than using named fields in the format string. 0.manufacturer 0.model .format aircraft Here we have used a single positional argument and used named tuple attribute names as field names in the format string. This is much clearer than just using positional arguments alone but it is a pity that we must specify the positional value even when using Python 3.1 . Fortunately there is a nicer way. Named tuples have a few private methods that is methods whose name begins with a leading underscore. One of them namedtuple._asdict is so useful that we will show it in action. manufacturer model .format aircraft._asdict Us- The private namedtuple._asdict method returns a mapping of key-value ing str pairs where each key is the name of a tuple element and each value is the cor- format with map- ----------------- ping un- Private methods such as namedtuple._asdict are not guaranteed to be available in all Python 3.x packing versions

Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.