1. Trang chủ
  2. » Luận Văn - Báo Cáo

Bai ch dn python bai ch dn python

122 0 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Bài dẫn Python http://www.vithon.org/tutorial/2.5/tut.html Bài dẫn Python Bài dẫn Python Guido van Rossum Python Software Foundation Thư điện tử: docs@python.org Fred L Drake, Jr., biên tập viên Nguyến Thành Nam, Lê Hồng Việt Lương Trọng Đức nhóm Python cho người Việt dịch Phiên 2.5 Ngày 19, tháng 09, năm 2006 Phiên 2.5, tài liệu cập nhật ngày 19, tháng 09, năm 2006 Xem Về tài liệu cách đề nghị thay đổi of 08/31/2011 03:19 PM Lời tựa http://www.vithon.org/tutorial/2.5/node1.html Bài dẫn Python Lời tựa Bản quyền © 2001-2006 Python Software Foundation Giữ toàn quyền Bản quyền © 2000 BeOpen.com Giữ toàn quyền Bản quyền © 1995-2000 Corporation for National Research Initiatives Giữ toàn quyền Bản quyền © 1991-1995 Stichting Mathematisch Centrum Giữ toàn quyền Xem phần cuối tài liệu tồn thơng tin quyền hạn giấy phép Tóm tắt: Python ngơn ngữ dễ học, mạnh mẽ Nó có cấu trúc liệu cấp cao hiệu hướng lập trình đối tượng đơn giản Cú pháp tao nhã kiểu liệu động Python, với chất thơng dịch biến thành ngơn ngữ bậc để viết kịch (scripting) phát triển ứng dụng nhanh nhiều lĩnh vực hầu hết hệ thống Trình thơng dịch Python thư viện chuẩn đầy đủ cung cấp miễn phí dạng nguồn nhị phân cho hệ thống từ trang chủ Python, http://www.python.org/, phát tán tùy thích Trang chủ phân phối liên kết nhiều mơ-đun Python khác, chương trình cơng cụ, tài liệu thêm Trình thơng dịch Python mở rộng dễ dàng với chức kiểu liệu viết C C++ (hoặc ngơn ngữ gọi từ C) Python phù hợp dùng làm ngôn ngữ mở rộng cho ứng dụng mà người dùng cải biến Bài dẫn giới thiệu với người đọc cách dễ hiểu khái niệm tính ngơn ngữ hệ thống Python Để tận dụng tốt dẫn này, bạn nên có trình thơng dịch Python sẵn sàng để thực tập Nhưng bạn không thiết cần đến để đọc tài liệu ví dụ ngắn dễ hiểu Để tìm hiểu thêm mơ-đun đối tượng chuẩn, xem qua tài liệu Tham khảo thư viện Python Sổ tay tham khảo Python chứa định nghĩa ngôn ngữ quy Để viết phần mở rộng C C++, bạn nên đọc Mở rộng Nhúng trình thơng dịch Python Tham khảo API cho Python/C Và có nhiều sách khác nói sâu Python Bài dẫn khơng nhằm vào việc nói tính năng, chí of 08/31/2011 03:24 PM Lời tựa http://www.vithon.org/tutorial/2.5/node1.html tính hay dùng Thay vào đó, giới thiệu nhiều chức đáng lưu ý Python đem lại cho bạn cách nhìn kiểu cách hương vị ngôn ngữ Sau đọc xong, bạn đọc viết mơ-đun chương trình Python, bạn sẵn sàng tìm hiểu tiếp mô-đun Python khác nhắc đến Tham khảo thư viện Python Phiên 2.5, tài liệu cập nhật ngày 19, tháng 09, năm 2006 Xem Về tài liệu cách đề nghị thay đổi of 08/31/2011 03:24 PM Mục lục http://www.vithon.org/tutorial/2.5/node2.html Bài dẫn Python Mục lục Khai vị Sừ dụng trình thơng dịch Python 2.1 Chạy trình thơng dịch 2.1.1 Truyền thơng số 2.1.2 Chế độ tương tác 2.2 Trình thơng dịch mơi trường 2.2.1 Xừ lý lỗi 2.2.2 Các kịch Python khả thi 2.2.3 Bảng mã mã nguồn 2.2.4 Tập tin khởi tạo tương tác Giới thiệu sơ Python 3.1 Dùng Python máy tính 3.1.1 Số 3.1.2 Chuỗi 3.1.3 Chuỗi Unicode 3.1.4 Danh sách 3.2 Những bước đầu lập trình Bàn thêm luồng điều khiển 4.1 Câu lệnh if 4.2 Câu lệnh for 4.3 Hàm range() 4.4 Câu lệnh break continue, vế else vòng lặp 4.5 Câu lệnh pass 4.6 Định nghĩa hàm 4.7 Bàn thêm định nghĩa hàm 4.7.1 Giá trị thông số mặc định 4.7.2 Thơng số từ khóa 4.7.3 Danh sách thơng số 4.7.4 Tháo danh sách thông số 4.7.5 Dạng lambda 4.7.6 Chuỗi tài liệu Cấu trúc liệu 5.1 Bàn thêm danh sách 5.1.1 Dùng danh sách ngăn xếp 5.1.2 Dùng danh sách hàng đợi 5.1.3 Cơng cụ lập trình hướng hàm 5.1.4 Gộp danh sách 5.2 Câu lệnh del 5.3 Bộ dãy 5.4 Tập hợp of 08/31/2011 03:20 PM Mục lục http://www.vithon.org/tutorial/2.5/node2.html 5.5 Từ điển 5.6 Kỹ thuật lặp 5.7 Bàn thêm điều kiện 5.8 So sánh dãy kiểu khác Mô-đun 6.1 Bàn thêm mô-đun 6.1.1 Đường dẫn tìm mơ-đun 6.1.2 Các tập tin Python ``đã dịch'' 6.2 Các mô-đun chuẩn 6.3 Hàm dir() 6.4 Gói 6.4.1 Nhập * từ gói 6.4.2 Tham chiếu nội gói 6.4.3 Gói nhiều thư mục Vào 7.1 Định dạng đẹp 7.2 Đọc viết tập tin 7.2.1 Phương thức đối tượng tập tin 7.2.2 Mô-đun pickle Lỗi biệt lệ 8.1 Lỗi cú pháp 8.2 Biệt lệ 8.3 Xừ lý biệt lệ 8.4 Nâng biệt lệ 8.5 Biệt lệ tự định nghĩa 8.6 Định nghĩa cách xừ lý 8.7 Định nghĩa xừ lý có sẵn Lớp 9.1 Vài lời thuật ngữ 9.2 Phạm vi Python vùng tên 9.3 Cái nhìn lớp 9.3.1 Cú pháp định nghĩa lớp 9.3.2 Đối tượng lớp 9.3.3 Đối tượng trường hợp 9.3.4 Đối tượng phương thức 9.4 Một vài lời bình 9.5 Kế thừa 9.5.1 Đa kế thừa 9.6 Biến riêng 9.7 Những điều khác 9.8 Biệt lệ lớp 9.9 Bộ lặp 9.10 Bộ tạo 9.11 Biểu thức tạo 10 Giới thiệu sơ thư viện chuẩn 10.1 Giao tiếp với hệ thống 10.2 Ký tự thay tập tin of 08/31/2011 03:20 PM Mục lục http://www.vithon.org/tutorial/2.5/node2.html 10.3 Thơng số dịng lệnh 10.4 Chuyển hướng luồng kết thúc chương trình 10.5 Khớp mẫu chuỗi 10.6 Tốn học 10.7 Truy cập internet 10.8 Ngày 10.9 Nén liệu 10.10 Đo lường hiệu suất 10.11 Quản lý chất lượng 10.12 Kèm pin 11 Giới thiệu sơ thư viện chuẩn - Phần II 11.1 Định dạng 11.2 Tạo mẫu 11.3 Làm việc với ghi liệu nhị phân 11.4 Đa luồng 11.5 Nhật ký 11.6 Tham chiếu yếu 11.7 Công cụ làm việc với danh sách 11.8 Số học dấu chấm động thập phân 12 Tiếp theo? A Soạn thảo tương tác Thay theo lịch sừ A.1 Soạn thảo dòng A.2 Thay theo lịch sừ A.3 Phím nóng A.4 Chú thích B Số học dấu chấm động: Vấn đề Giới hạn B.1 Lỗi biểu diễn C Lịch sừ Giấy phép C.1 Lịch sừ phần mềm C.2 Điều khoản truy cập sừ dụng Python C.3 Giấy phép công nhận phần mềm kèm theo C.3.1 Mersenne Twister C.3.2 Sockets C.3.3 Điều khiển biệt lệ dấu chấm động C.3.4 Thuật tốn hàm băm MD5 C.3.5 Dịch vụ socket khơng đồng C.3.6 Quản lý cookie C.3.7 Profiling C.3.8 Theo dõi hoạt động C.3.9 Chức UUencode UUdecode C.3.10 Gọi thủ tục xa qua XML D Thuật ngữ Chỉ mục Phiên 2.5, tài liệu cập nhật ngày 19, tháng 09, năm 2006 Xem Về tài liệu cách đề nghị thay đổi of 08/31/2011 03:20 PM Khai vị http://www.vithon.org/tutorial/2.5/node3.html Bài dẫn Python Khai vị Nếu bạn làm việc nhiều với máy vi tính, lúc bạn nhận thấy bạn muốn tự động hóa số việc Ví dụ, bạn muốn thực phép tìm kiếm thay với nhiều tập tin văn bản, đổi tên xếp loạt tập tin ảnh theo cách phức tạp Có thể bạn muốn viết sở liệu tùy biến nho nhỏ, ứng dụng với giao diện đồ họa đặc biệt, hay trò chơi đơn giản Nếu bạn người chuyên viết phần mềm, bạn làm việc với nhiều thư viện C/C++/Java bạn nhận thấy thường lặp lặp lại việc viết/biên dịch/thử/biên dịch tốn thời gian Có thể bạn viết thử nghiệm cho thư viện nhận viết mã lệnh để thử nghiệm việc chán ngấy Hoặc bạn viết chương trình cần sử dụng ngôn ngữ mở rộng, bạn không muốn thiết kế, xây dựng ngôn ngữ cho ứng dụng Python ngơn ngữ lập trình bạn cần Bạn viết kịch UNIX bó lệnh (batch file) Windows cho công việc kiểu nhưng, ngôn ngữ kịch tốt cho việc chuyển tập tin lòng vịng sửa đổi liệu văn bản, khơng thích hợp cho ứng dụng với giao diện đồ họa trò chơi Bạn cần viết chương trình C/C++/Java, tiêu tốn nhiều thời gian cho việc phát triển chí từ nháp chương trình Sử dụng Python đơn giản hơn, chạy Windows, MacOS X, hệ điều hành UNIX , giúp bạnh hồn thành cơng việc nhanh Sử dụng Python đơn giản, ngơn ngữ lập trình thực thụ, cung cấp nhiều cấu trúc hỗ trợ chương trình lớn so với ngơn ngữ kịch bó lệnh Windows Mặt khác, Python hỗ trợ nhiều phép kiểm tra lỗi C, và, ngơn ngữ bậc-rất-cao, có sẵn kiểu liệu cấp cao, mảng từ điển linh hoạt Chính nhiều kiểu liệu tổng quát Python ứng dụng rộng rãi Awk chí Perl nhiều loại cơng việc khác nhau, có nhiều việc làm Python dễ dàng làm ngôn ngữ khác Python cho phép bạn chia nhỏ chương trình thành mơ-đun để sử dụng lại chương trình Python khác Nó có sẵn nhiều mơ-đun chuẩn để bạn sử dụng làm sở cho chương trình ví dụ để bắt đầu học lập trình Python Một vài mơ-đun số chúng cung cấp chức tập tin I/O (vào/ra), lệnh gọi hàm hệ thống, socket, chí giao tiếp với cơng cụ giao diện đồ họa Tk of 08/31/2011 07:36 AM Khai vị http://www.vithon.org/tutorial/2.5/node3.html Python ngơn ngữ thơng dịch, điều giúp bạn tiết kiệm thời gian trình phát triển chương trình việc biên dịch hay liên kết không cần thiết Bộ thơng dịch dùng cách tương tác, làm cho việc thử nghiệm tính ngơn ngữ trở nên dễ dàng, viết chương trình bỏ đi, thử chức việc phát triển chương trình từ lên Nó máy tính cầm tay tiện lợi Python cho phép viết chương trình nhỏ gọn dễ hiểu Các chương trình viết Python thường ngắn so với chương trình viết C, C++ Java, nhiều lý do: kiểu liệu cao cấp cho phép bạn thực nhanh thao tác phức tạp với lệnh đơn giản; phát biểu lệnh nhóm lại khoảng cách thụt đầu dịng thay đóng mở với dấu ngoặc; không cần khai báo biến tham số trước sử dụng Python có tính mở rộng: bạn biết lập trình C dễ để bổ sung hàm có sẵn mơ-đun vào thông dịch, việc thực thao tác quan trọng tốc độ tối đa, liên kết chương trình Python với thư viện cung cấp dạng nhị phân (ví dụ thư viện đồ họa vài nhà sản xuất) Một bạn thực móc nối, bạn liên kết thơng dịch Python vào ứng dụng viết C sử dụng tính mở rộng ngơn ngữ lệnh cho ứng dụng Cũng xin nói ln, tên ngôn ngữ đặt sau BBC phát chương trình ``Monty Python's Flying Circus'' khơng có liên quan với lồi bị sát bẩn thỉu Những tham khảo mang tính trào phùng tới Monty Python tài liệu không cho phép, mà cổ vũ Bây tất bạn bị kích thích Python, bạn muốn khám phá kỹ Cách học ngơn ngữ tốt sử dụng nó, dẫn mời gọi bạn vừa thử trình thơng dịch Python bạn vừa đọc Trong chương tiếp theo, phương thức sử dụng thông dịch giải thích Điều khơng đơn thơng tin, cịn cho việc thử ví dụ trình bày sau Phần tự học cịn lại giới thiệu tính khác ngơn ngữ Python hệ thống thơng qua ví dụ, bắt đầu với biểu thức đơn giản, câu lệnh kiểu liệu, qua hàm mô-đun, kết thúc tiếp cận với khái niệm cao cấp biệt lệ lớp người dùng tự định nghĩa Phiên 2.5, tài liệu cập nhật ngày 19, tháng 09, năm 2006 Xem Về tài liệu cách đề nghị thay đổi of 08/31/2011 07:36 AM Sử dụng trình thơng dịch Python http://www.vithon.org/tutorial/2.5/node4.html Bài dẫn Python Sử dụng trình thơng dịch Python 2.1 Chạy trình thơng dịch Bộ thơng dịch Python thường cài đặt /usr/local/bin/python máy tính cài đặt sẵn; bổ sung /usr/local/bin vào đường dẫn tìm kiếm vỏ (shell) UNIX giúp khởi động từ nơi lệnh đơn giản python vỏ Vì nơi mà trình thơng dịch cài đặt tùy chọn nên cài đặt trình thơng dịch đặt nơi khác; hỏi quản trị hệ thống bạn (ví dụ /usr/local/python vị trí hay dùng để cài.) Trên máy tính dùng Windows, Python thường cài đặt vào C:\Python24, dù bạn thay đổi vị trí cài đặt chạy chương trình cài đặt Để bổ sung thư mục vào đường dẫn, bạn gõ lệnh sau lên dấu nhắc lệnh cửa sổ DOS: set path=%path%;C:\python24 Gõ ký tự kết thúc tập tin (end-of-file character) (Control-D UNIX, Control-Z Windows) dấu nhắc thơng dịch khỏi thông dịch trả trạng thái kết thúc chương trình (khơng) cho hệ điều hành, bạn khỏi thơng dịch lệnh sau: "import sys; sys.exit()" Tính soạn thảo theo dịng thơng dịch thường khơng phức tạp Trên UNIX, cài đặt thơng dịch bật chế độ hỗ trợ cho thư viện GNU readline, điều bổ sung tính soạn thảo tương tác tốt tính lịch sử lệnh Có thể kiểm tra việc hỗ trợ tính soạn thảo dịng cách nhấn Control-P dấu nhắc Python Nếu có tiếng bíp, thơng dịch bạn có hỗ trợ soạn thảo dòng; xem phụ lục A để biết phím Nếu khơng có xảy ra, kí tự P lên, tính soạn thảo dịng khơng hỗ trợ; bạn việc dùng phím lùi (backspace) để xóa kí tự Bộ thơng dịch Python hoạt động giống với vỏ UNIX : gọi với đầu nhập chuẩn kết nối với thiết bị đầu cuối (tty device), đọc thực lệnh cách tương tác; gọi với tham số tên tập tin với đầu vào chuẩn tập tin, đọc thực kịch chứa tập of 08/31/2011 07:37 AM Sử dụng trình thơng dịch Python http://www.vithon.org/tutorial/2.5/node4.html Một cách để khởi động thông dịch lệnh "python -c command [arg] ", thực thi nhiều câu lệnh command, giống tùy chọn -c vỏ Vì câu lệnh Python thường chứa khoảng trống kí tự đặc biệt, chọn lựa an tồn bao command dấu nháy kép (") Một số mơ-đun dùng kịch Chúng gọi cách sử dụng cú pháp "python -m module [arg] ", lệnh thực tập tin nguồn module bạn tên tập tin đường dẫn đầy đủ dòng lệnh Xin lưu ý có khác biệt "python file" "python >> "); với dịng tiếp nối (continuation line), nhắc với dấu nhắc thứ (secondary prompt), mặc định ba dấu chấm (" ") Bộ thông dịch in thông báo chào mừng, số hiệu phiên thông báo quyền trước dấu nhắc: of 08/31/2011 07:37 AM C History and License http://www.vithon.org/tutorial/2.5/node17.html on the Internet using the following unique, persistent identifier (known as a handle): 1895.22/1013 This Agreement may also be obtained from a proxy server on the Internet using the following URL: http://hdl.handle.net/1895.22/1013.'' In the event Licensee prepares a derivative work that is based on or incorporates Python 1.6.1 or any part thereof, and wants to make the derivative work available to others as provided herein, then Licensee hereby agrees to include in any such work a brief summary of the changes made to Python 1.6.1 CNRI is making Python 1.6.1 available to Licensee on an ``AS IS'' basis CNRI MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED BY WAY OF EXAMPLE, BUT NOT LIMITATION, CNRI MAKES NO AND DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON 1.6.1 WILL NOT INFRINGE ANY THIRD PARTY RIGHTS CNRI SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON 1.6.1 FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 1.6.1, OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF This License Agreement will automatically terminate upon a material breach of its terms and conditions This License Agreement shall be governed by the federal intellectual property law of the United States, including without limitation the federal copyright law, and, to the extent such U.S federal law does not apply, by the law of the Commonwealth of Virginia, excluding Virginia's conflict of law provisions Notwithstanding the foregoing, with regard to derivative works based on Python 1.6.1 that incorporate non-separable material that was previously distributed under the GNU General Public License (GPL), the law of the Commonwealth of Virginia shall govern this License Agreement only as to issues arising under or with respect to Paragraphs 4, 5, and of this License Agreement Nothing in this License Agreement shall be deemed to create any relationship of agency, partnership, or joint venture between CNRI and Licensee This License Agreement does not grant permission to use CNRI trademarks or trade name in a trademark sense to endorse or promote products or services of Licensee, or any third party By clicking on the ``ACCEPT'' button where indicated, or by copying, installing or otherwise using Python 1.6.1, Licensee agrees to be bound by the terms and conditions of this License Agreement ACCEPT of 13 08/31/2011 10:54 AM C History and License http://www.vithon.org/tutorial/2.5/node17.html CWI LICENSE AGREEMENT FOR PYTHON 0.9.0 THROUGH 1.2 Copyright © 1991 - 1995, Stichting Mathematisch Centrum Amsterdam, The Netherlands All rights reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the name of Stichting Mathematisch Centrum or CWI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE C.3 Licenses and Acknowledgements for Incorporated Software This section is an incomplete, but growing list of licenses and acknowledgements for third-party software incorporated in the Python distribution C.3.1 Mersenne Twister The _random module includes code based on a download from http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html The following are the verbatim comments from the original code: A C-program for MT19937, with initialization improved 2002/1/26 Coded by Takuji Nishimura and Makoto Matsumoto Before using, initialize the state by using init_genrand(seed) or init_by_array(init_key, key_length) Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions of 13 08/31/2011 10:54 AM C History and License http://www.vithon.org/tutorial/2.5/node17.html are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE Any feedback is very welcome http://www.math.keio.ac.jp/matumoto/emt.html email: matumoto@math.keio.ac.jp C.3.2 Sockets The socket module uses the functions, getaddrinfo, and getnameinfo, which are coded in separate source files from the WIDE Project, http://www.wide.ad.jp/about/index.html Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project All rights reserved Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution Neither the name of the project nor the names of its contributors may be used to endorse or promote products derived from this software of 13 08/31/2011 10:54 AM C History and License http://www.vithon.org/tutorial/2.5/node17.html without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND GAI_ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE FOR GAI_ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON GAI_ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN GAI_ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE C.3.3 Floating point exception control The source for the fpectl module includes the following notice: / Copyright (c) 1996 \ | The Regents of the University of California | | All rights reserved | | | | Permission to use, copy, modify, and distribute this software for | | any purpose without fee is hereby granted, provided that this en| | tire notice is included in all copies of any software which is or | | includes a copy or modification of this software and in all | | copies of the supporting documentation for such software | | | | This work was produced at the University of California, Lawrence | | Livermore National Laboratory under contract no W-7405-ENG-48 | | between the U.S Department of Energy and The Regents of the | | University of California for the operation of UC LLNL | | | | DISCLAIMER | | | | This software was prepared as an account of work sponsored by an | | agency of the United States Government Neither the United States | | Government nor the University of California nor any of their em| | ployees, makes any warranty, express or implied, or assumes any | | liability or responsibility for the accuracy, completeness, or | | usefulness of any information, apparatus, product, or process | | disclosed, or represents that its use would not infringe | | privately-owned rights Reference herein to any specific commer| | cial products, process, or service by trade name, trademark, | | manufacturer, or otherwise, does not necessarily constitute or | | imply its endorsement, recommendation, or favoring by the United | | States Government or the University of California The views and | | opinions of authors expressed herein not necessarily state or | | reflect those of the United States Government or the University | of 13 08/31/2011 10:54 AM C History and License http://www.vithon.org/tutorial/2.5/node17.html | of California, and shall not be used for advertising or product | \ endorsement purposes / - C.3.4 MD5 message digest algorithm The source code for the md5 module contains the following notice: Copyright (C) 1999, 2002 Aladdin Enterprises All rights reserved This software is provided 'as-is', without any express or implied warranty In no event will the authors be held liable for any damages arising from the use of this software Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: The origin of this software must not be misrepresented; you must not claim that you wrote the original software If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software This notice may not be removed or altered from any source distribution L Peter Deutsch ghost@aladdin.com Independent implementation of MD5 (RFC 1321) This code implements the MD5 Algorithm defined in RFC 1321, whose text is available at http://www.ietf.org/rfc/rfc1321.txt The code is derived from the text of the RFC, including the test suite (section A.5) but excluding the rest of Appendix A It does not include any code or documentation that is identified in the RFC as being copyrighted The original and principal author of md5.h is L Peter Deutsch Other authors are noted in the change history that follows (in reverse chronological order): 2002-04-13 lpd Removed support for non-ANSI compilers; removed references to Ghostscript; clarified derivation from RFC 1321; now handles byte order either statically or dynamically 1999-11-04 lpd Edited comments slightly for automatic TOC extraction 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5); added conditionalization for C++ compilation from Martin Purschke of 13 08/31/2011 10:54 AM C History and License http://www.vithon.org/tutorial/2.5/node17.html 1999-05-03 lpd Original version C.3.5 Asynchronous socket services The asynchat and asyncore modules contain the following notice: Copyright 1996 by Sam Rushing All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the name of Sam Rushing not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE C.3.6 Cookie management The Cookie module contains the following notice: Copyright 2000 by Timothy O'Malley All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the name of Timothy O'Malley not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission Timothy O'Malley DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL Timothy O'Malley BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 10 of 13 08/31/2011 10:54 AM C History and License http://www.vithon.org/tutorial/2.5/node17.html WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE C.3.7 Profiling The profile and pstats modules contain the following notice: Copyright 1994, by InfoSeek Corporation, all rights reserved Written by James Roskind Permission to use, copy, modify, and distribute this Python software and its associated documentation for any purpose (subject to the restriction in the following sentence) without fee is hereby granted, provided that the above copyright notice appears in all copies, and that both that copyright notice and this permission notice appear in supporting documentation, and that the name of InfoSeek not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission This permission is explicitly restricted to the copying and modification of the software to remain in Python, compiled Python, or other languages (such as C) wherein the modified or derived code is exclusively imported into a Python module INFOSEEK CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS IN NO EVENT SHALL INFOSEEK CORPORATION BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE C.3.8 Execution tracing The trace module contains the following notice: portions copyright 2001, Autonomous Zones Industries, Inc., all rights err reserved and offered to the public under the terms of the Python 2.2 license Author: Zooko O'Whielacronx http://zooko.com/ mailto:zooko@zooko.com Copyright 2000, Mojam Media, Inc., all rights reserved Author: Skip Montanaro Copyright 1999, Bioreason, Inc., all rights reserved Author: Andrew Dalke Copyright 1995-1997, Automatrix, Inc., all rights reserved 11 of 13 08/31/2011 10:54 AM C History and License http://www.vithon.org/tutorial/2.5/node17.html Author: Skip Montanaro Copyright 1991-1995, Stichting Mathematisch Centrum, all rights reserved Permission to use, copy, modify, and distribute this Python software and its associated documentation for any purpose without fee is hereby granted, provided that the above copyright notice appears in all copies, and that both that copyright notice and this permission notice appear in supporting documentation, and that the name of neither Automatrix, Bioreason or Mojam Media be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission C.3.9 UUencode and UUdecode functions The uu module contains the following notice: Copyright 1994 by Lance Ellinghouse Cathedral City, California Republic, United States of America All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the name of Lance Ellinghouse not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE CENTRUM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE Modified by Jack Jansen, CWI, July 1995: - Use binascii module to the actual line-by-line conversion between ascii and binary This results in a 1000-fold speedup The C version is still times faster, though - Arguments more compliant with python standard C.3.10 XML Remote Procedure Calls The xmlrpclib module contains the following notice: The XML-RPC client interface is Copyright (c) 1999-2002 by Secret Labs AB Copyright (c) 1999-2002 by Fredrik Lundh 12 of 13 08/31/2011 10:54 AM C History and License http://www.vithon.org/tutorial/2.5/node17.html By obtaining, using, and/or copying this software and/or its associated documentation, you agree that you have read, understood, and will comply with the following terms and conditions: Permission to use, copy, modify, and distribute this software and its associated documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appears in all copies, and that both that copyright notice and this permission notice appear in supporting documentation, and that the name of Secret Labs AB or the author not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE Release 2.5, documentation updated on 19th September, 2006 See About this document for information on suggesting changes 13 of 13 08/31/2011 10:54 AM D Glossary http://www.vithon.org/tutorial/2.5/node18.html Python Tutorial This Appendix was left untranslated D Glossary >>> The typical Python prompt of the interactive shell Often seen for code examples that can be tried right away in the interpreter The typical Python prompt of the interactive shell when entering code for an indented code block BDFL Benevolent Dictator For Life, a.k.a Guido van Rossum, Python's creator byte code The internal representation of a Python program in the interpreter The byte code is also cached in pyc and pyo files so that executing the same file is faster the second time (recompilation from source to byte code can be avoided) This ``intermediate language'' is said to run on a ``virtual machine'' that calls the subroutines corresponding to each bytecode classic class Any class which does not inherit from object See new-style class coercion The implicit conversion of an instance of one type to another during an operation which involves two arguments of the same type For example, int(3.15) converts the floating point number to the integer 3, but in 3+4.5, each argument is of a different type (one int, one float), and both must be converted to the same type before they can be added or it will raise a TypeError Coercion between two operands can be performed with the coerce builtin function; thus, 3+4.5 is equivalent to calling operator.add(*coerce(3, 4.5)) and results in operator.add(3.0, 4.5) Without coercion, all arguments of even compatible types would have to be normalized to the same value by the programmer, e.g., float(3)+4.5 rather than just 3+4.5 complex number An extension of the familiar real number system in which all numbers are expressed as a sum of a real part and an imaginary part Imaginary numbers are real multiples of the imaginary unit (the square root of -1), often written i in mathematics or j in engineering Python has builtin support for complex numbers, which are written with this latter notation; the imaginary part is written with a j suffix, e.g., 3+1j To get access to complex equivalents of the math module, use cmath Use of complex numbers is a fairly advanced mathematical feature If you're not aware of a need for them, it's almost certain you can safely ignore them of 08/31/2011 10:59 AM D Glossary http://www.vithon.org/tutorial/2.5/node18.html descriptor Any new-style object that defines the methods get (), set (), or delete () When a class attribute is a descriptor, its special binding behavior is triggered upon attribute lookup Normally, writing a.b looks up the object b in the class dictionary for a, but if b is a descriptor, the defined method gets called Understanding descriptors is a key to a deep understanding of Python because they are the basis for many features including functions, methods, properties, class methods, static methods, and reference to super classes dictionary An associative array, where arbitrary keys are mapped to values The use of dict much resembles that for list, but the keys can be any object with a hash () function, not just integers starting from zero Called a hash in Perl duck-typing Pythonic programming style that determines an object's type by inspection of its method or attribute signature rather than by explicit relationship to some type object ("If it looks like a duck and quacks like a duck, it must be a duck.") By emphasizing interfaces rather than specific types, well-designed code improves its flexibility by allowing polymorphic substitution Duck-typing avoids tests using type() or isinstance() Instead, it typically employs hasattr() tests or EAFP programming EAFP Easier to ask for forgiveness than permission This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false This clean and fast style is characterized by the presence of many try and except statements The technique contrasts with the LBYL style that is common in many other languages such as C future A pseudo module which programmers can use to enable new language features which are not compatible with the current interpreter For example, the expression 11/4 currently evaluates to If the module in which it is executed had enabled true division by executing: from future import division the expression 11/4 would evaluate to 2.75 By importing the future module and evaluating its variables, you can see when a new feature was first added to the language and when it will become the default: >>> import future >>> future .division _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192) generator A function that returns an iterator It looks like a normal function except that values are returned to the caller using a yield statement instead of a return statement Generator functions often contain one or more for or while loops that yield elements back to the caller The function execution is stopped at the yield keyword (returning the result) and is resumed there when the next of 08/31/2011 10:59 AM D Glossary http://www.vithon.org/tutorial/2.5/node18.html element is requested by calling the next() method of the returned iterator generator expression An expression that returns a generator It looks like a normal expression followed by a for expression defining a loop variable, range, and an optional if expression The combined expression generates values for an enclosing function: >>> sum(i*i for i in range(10)) 285 GIL # sum of squares 0, 1, 4, 81 See global interpreter lock global interpreter lock The lock used by Python threads to assure that only one thread can be run at a time This simplifies Python by assuring that no two processes can access the same memory at the same time Locking the entire interpreter makes it easier for the interpreter to be multi-threaded, at the expense of some parallelism on multi-processor machines Efforts have been made in the past to create a ``free-threaded'' interpreter (one which locks shared data at a much finer granularity), but performance suffered in the common single-processor case IDLE An Integrated Development Environment for Python IDLE is a basic editor and interpreter environment that ships with the standard distribution of Python Good for beginners, it also serves as clear example code for those wanting to implement a moderately sophisticated, multi-platform GUI application immutable An object with fixed value Immutable objects are numbers, strings or tuples (and more) Such an object cannot be altered A new object has to be created if a different value has to be stored They play an important role in places where a constant hash value is needed, for example as a key in a dictionary integer division Mathematical division discarding any remainder For example, the expression 11/4 currently evaluates to in contrast to the 2.75 returned by float division Also called floor division When dividing two integers the outcome will always be another integer (having the floor function applied to it) However, if one of the operands is another numeric type (such as a float), the result will be coerced (see coercion) to a common type For example, an integer divided by a float will result in a float value, possibly with a decimal fraction Integer division can be forced by using the // operator instead of the / operator See also future interactive Python has an interactive interpreter which means that you can try out things and immediately see their results Just launch python with no arguments (possibly by selecting it from your computer's main menu) It is a very powerful way to test out new ideas or inspect modules and packages (remember help(x)) of 08/31/2011 10:59 AM D Glossary http://www.vithon.org/tutorial/2.5/node18.html interpreted Python is an interpreted language, as opposed to a compiled one This means that the source files can be run directly without first creating an executable which is then run Interpreted languages typically have a shorter development/debug cycle than compiled ones, though their programs generally also run more slowly See also interactive iterable A container object capable of returning its members one at a time Examples of iterables include all sequence types (such as list, str, and tuple) and some non-sequence types like dict and file and objects of any classes you define with an iter () or getitem () method Iterables can be used in a for loop and in many other places where a sequence is needed (zip(), map(), ) When an iterable object is passed as an argument to the builtin function iter(), it returns an iterator for the object This iterator is good for one pass over the set of values When using iterables, it is usually not necessary to call iter() or deal with iterator objects yourself The for statement does that automatically for you, creating a temporary unnamed variable to hold the iterator for the duration of the loop See also iterator, sequence, and generator iterator An object representing a stream of data Repeated calls to the iterator's next() method return successive items in the stream When no more data is available a StopIteration exception is raised instead At this point, the iterator object is exhausted and any further calls to its next() method just raise StopIteration again Iterators are required to have an iter () method that returns the iterator object itself so every iterator is also iterable and may be used in most places where other iterables are accepted One notable exception is code that attempts multiple iteration passes A container object (such as a list) produces a fresh new iterator each time you pass it to the iter() function or use it in a for loop Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container LBYL Look before you leap This coding style explicitly tests for pre-conditions before making calls or lookups This style contrasts with the EAFP approach and is characterized by the presence of many if statements list comprehension A compact way to process all or a subset of elements in a sequence and return a list with the results result = ["0x%02x" % x for x in range(256) if x % == 0] generates a list of strings containing hex numbers (0x ) that are even and in the range from to 255 The if clause is optional If omitted, all elements in range(256) are processed mapping A container object (such as dict) that supports arbitrary key lookups using the special method getitem () metaclass The class of a class Class definitions create a class name, a class dictionary, of 08/31/2011 10:59 AM D Glossary http://www.vithon.org/tutorial/2.5/node18.html and a list of base classes The metaclass is responsible for taking those three arguments and creating the class Most object oriented programming languages provide a default implementation What makes Python special is that it is possible to create custom metaclasses Most users never need this tool, but when the need arises, metaclasses can provide powerful, elegant solutions They have been used for logging attribute access, adding thread-safety, tracking object creation, implementing singletons, and many other tasks mutable Mutable objects can change their value but keep their id() See also immutable namespace The place where a variable is stored Namespaces are implemented as dictionaries There are the local, global and builtin namespaces as well as nested namespaces in objects (in methods) Namespaces support modularity by preventing naming conflicts For instance, the functions builtin .open() and os.open() are distinguished by their namespaces Namespaces also aid readability and maintainability by making it clear which module implements a function For instance, writing random.seed() or itertools.izip() makes it clear that those functions are implemented by the random and itertools modules respectively nested scope The ability to refer to a variable in an enclosing definition For instance, a function defined inside another function can refer to variables in the outer function Note that nested scopes work only for reference and not for assignment which will always write to the innermost scope In contrast, local variables both read and write in the innermost scope Likewise, global variables read and write to the global namespace new-style class Any class that inherits from object This includes all built-in types like list and dict Only new-style classes can use Python's newer, versatile features like slots , descriptors, properties, getattribute (), class methods, and static methods Python3000 A mythical python release, not required to be backward compatible, with telepathic interface slots A declaration inside a new-style class that saves memory by pre-declaring space for instance attributes and eliminating instance dictionaries Though popular, the technique is somewhat tricky to get right and is best reserved for rare cases where there are large numbers of instances in a memory-critical application sequence An iterable which supports efficient element access using integer indices via the getitem () and len () special methods Some built-in sequence types are list, str, tuple, and unicode Note that dict also supports getitem () and len (), but is considered a mapping rather than a of 08/31/2011 10:59 AM D Glossary http://www.vithon.org/tutorial/2.5/node18.html sequence because the lookups use arbitrary immutable keys rather than integers Zen of Python Listing of Python design principles and philosophies that are helpful in understanding and using the language The listing can be found by typing ``import this'' at the interactive prompt Release 2.5, documentation updated on 19th September, 2006 See About this document for information on suggesting changes of 08/31/2011 10:59 AM ... phân cho hệ thống từ trang ch? ?? Python, http://www .python. org/, phát tán tùy th? ?ch Trang ch? ?? phân phối liên kết nhiều mơ-đun Python khác, ch? ?ơng trình cơng cụ, tài liệu thêm Trình thơng d? ?ch Python. .. th? ?ch hợp cho ứng dụng với giao diện đồ họa trò ch? ?i Bạn cần viết ch? ?ơng trình C/C++/Java, tiêu tốn nhiều thời gian cho việc phát triển ch? ? từ nháp ch? ?ơng trình Sử dụng Python đơn giản hơn, ch? ??y... chuẩn trình thơng d? ?ch Python Khi tập tin k? ?ch (script file) sử dụng, đơi hữu dụng ch? ??y ch? ?ơng trình chuyển sang ch? ?? độ tương tác sau Điều thực c? ?ch truyền -i trước script (python -i script) (Phương

Ngày đăng: 22/12/2022, 18:47

Xem thêm:

w