tailieunhanh - PROGRAMMING IN PYTHON 3 - PART 3

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 print Total 0 .2f .format total prints Total 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 Aircraft . manufacturer model seating Seating Seating minimum maximum aircraft Aircraft Airbus A320-200 Seating 100 220 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 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 we could reduce this format string to just . But this approach means that we must look at the arguments passed to to see what the replacement texts will be. This seems less clear than using named fields in the format string. .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 . 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 is so useful that we will show it in action. manufacturer model .format Us- The private 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 are not guaranteed to be available in all Python packing versions

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã 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.