Hướng dẫn 2to3 python - Trăn 2to3

2TO3 là một chương trình Python đọc mã nguồn Python 2.x và áp dụng một loạt các trình sửa chữa để chuyển đổi nó thành mã Python 3.x hợp lệ. Thư viện tiêu chuẩn chứa một tập hợp các trình sửa chữa phong phú sẽ xử lý hầu hết tất cả các mã. Tuy nhiên, 2TO3 Thư viện hỗ trợ lib2to3 là một thư viện linh hoạt và chung chung, vì vậy có thể viết trình sửa chữa của riêng bạn cho 2to3.

Sử dụng 2to3¶

2to3 thường sẽ được cài đặt với trình thông dịch Python dưới dạng tập lệnh. Nó cũng được đặt trong thư mục

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
0 của gốc Python.

Các đối số cơ bản của 2to3 là ​​một danh sách các tệp hoặc thư mục để chuyển đổi. Các thư mục được đệ quy đi qua các nguồn Python.

Dưới đây là tệp nguồn Python 2.x mẫu,

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
1:

def greet(name):
    print "Hello, {0}!".format(name)
print "What's your name?"
name = raw_input()
greet(name)

Nó có thể được chuyển đổi thành mã Python 3.x qua 2to3 trên dòng lệnh:

Một sự khác biệt so với tệp nguồn gốc được in. 2TO3 cũng có thể viết các sửa đổi cần thiết ngay vào tệp nguồn. (Một bản sao lưu của tệp gốc được thực hiện trừ khi

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
2 cũng được đưa ra.) Viết các thay đổi được bật lại bằng cờ
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
3:

Sau khi chuyển đổi,

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
1 trông như thế này:

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)

Nhận xét và thụt chính xác được bảo tồn trong suốt quá trình dịch thuật.

Theo mặc định, 2TO3 chạy một bộ trình sửa chữa được xác định trước. Cờ

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
5 liệt kê tất cả các trình sửa chữa có sẵn. Một tập hợp các trình sửa chữa rõ ràng để chạy có thể được đưa ra với
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
6. Tương tự như vậy,
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
7 vô hiệu hóa rõ ràng một trình sửa chữa. Ví dụ sau chỉ chạy bộ sửa lỗi
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
8 và
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
9:predefined fixers. The
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
5 flag lists all available fixers. An explicit set of fixers to run can be given with
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
6. Likewise the
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
7 explicitly disables a fixer. The following example runs only the
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
8 and
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
9 fixers:

$ 2to3 -f imports -f has_key example.py

Lệnh này chạy mọi trình sửa chữa ngoại trừ trình sửa chữa

$ 2to3 -f imports -f has_key example.py
0:

$ 2to3 -x apply example.py

Một số trình sửa chữa rõ ràng, có nghĩa là chúng không được chạy theo mặc định và phải được liệt kê trên dòng lệnh sẽ được chạy. Ở đây, ngoài trình sửa lỗi mặc định, trình sửa chữa

$ 2to3 -f imports -f has_key example.py
1 còn được chạy:

$ 2to3 -f all -f idioms example.py

Lưu ý cách vượt qua

$ 2to3 -f imports -f has_key example.py
2 cho phép tất cả các trình sửa chữa mặc định.

Đôi khi 2to3 sẽ tìm thấy một vị trí trong mã nguồn của bạn cần được thay đổi, nhưng 2to3 không thể tự động sửa chữa. Trong trường hợp này, 2to3 sẽ in cảnh báo bên dưới Diff cho một tệp. Bạn nên giải quyết cảnh báo để có mã 3.x tuân thủ.

2to3 cũng có thể tái cấu trúc tài liệu. Để kích hoạt chế độ này, hãy sử dụng cờ

$ 2to3 -f imports -f has_key example.py
3. Lưu ý rằng chỉ các tài liệu sẽ được tái cấu trúc. Điều này cũng không yêu cầu mô -đun phải là Python hợp lệ. Ví dụ, các ví dụ như tài liệu như trong tài liệu REST cũng có thể được tái cấu trúc với tùy chọn này.

Tùy chọn

$ 2to3 -f imports -f has_key example.py
4 cho phép đầu ra thêm thông tin về quy trình dịch.

Vì một số câu lệnh in có thể được phân tích cú pháp dưới dạng gọi chức năng hoặc câu lệnh, 2to3 không thể luôn đọc các tệp chứa hàm in. Khi 2TO3 phát hiện sự hiện diện của chỉ thị trình biên dịch

$ 2to3 -f imports -f has_key example.py
5, nó sẽ sửa đổi ngữ pháp nội bộ của nó để giải thích
$ 2to3 -f imports -f has_key example.py
6 là một hàm. Thay đổi này cũng có thể được kích hoạt thủ công bằng cờ
$ 2to3 -f imports -f has_key example.py
7. Sử dụng
$ 2to3 -f imports -f has_key example.py
7 để chạy bộ sửa lỗi trên mã đã có các câu lệnh in được chuyển đổi. Ngoài ra
$ 2to3 -f imports -f has_key example.py
9 có thể được sử dụng để biến
$ 2to3 -x apply example.py
0 thành một hàm.

Tùy chọn

$ 2to3 -x apply example.py
1 hoặc
$ 2to3 -x apply example.py
2 cho phép đặc điểm kỹ thuật của một thư mục thay thế cho các tệp đầu ra được xử lý được ghi vào. Cờ
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
2 được yêu cầu khi sử dụng điều này vì các tệp sao lưu không có ý nghĩa khi không ghi đè các tệp đầu vào.

Mới trong phiên bản 3.2.3: Tùy chọn

$ 2to3 -x apply example.py
1 đã được thêm vào.The
$ 2to3 -x apply example.py
1 option was added.

Cờ

$ 2to3 -x apply example.py
5 hoặc
$ 2to3 -x apply example.py
6 yêu cầu 2to3 luôn ghi các tệp đầu ra ngay cả khi không cần thay đổi cho tệp. Điều này hữu ích nhất với
$ 2to3 -x apply example.py
1 để toàn bộ cây nguồn Python được sao chép với bản dịch từ thư mục này sang thư mục khác. Tùy chọn này ngụ ý cờ
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
3 vì nó sẽ không có ý nghĩa khác.

Mới trong phiên bản 3.2.3: Cờ

$ 2to3 -x apply example.py
5 đã được thêm vào.The
$ 2to3 -x apply example.py
5 flag was added.

Tùy chọn

$ 2to3 -f all -f idioms example.py
0 chỉ định một chuỗi để nối vào tất cả các tên tệp đầu ra. Cờ
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
2 là bắt buộc khi chỉ định đây là bản sao lưu là không cần thiết khi viết cho các tên tệp khác nhau. Thí dụ:

$ 2to3 -n -W --add-suffix=3 example.py

Sẽ khiến một tệp được chuyển đổi có tên

$ 2to3 -f all -f idioms example.py
2 được viết.

Mới trong phiên bản 3.2.3: Tùy chọn

$ 2to3 -f all -f idioms example.py
0 đã được thêm vào.The
$ 2to3 -f all -f idioms example.py
0 option was added.

Để dịch toàn bộ dự án từ một cây thư mục sang sử dụng khác:

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode

Người sửa chữa trong

Mỗi bước của mã chuyển đổi được gói gọn trong một bộ sửa lỗi. Lệnh

$ 2to3 -f all -f idioms example.py
4 liệt kê chúng. Như đã ghi lại ở trên, mỗi người có thể được bật và tắt riêng lẻ. Chúng được mô tả ở đây chi tiết hơn.documented above, each can be turned on and off individually. They are described here in more detail.

________ 45¶

Loại bỏ việc sử dụng

$ 2to3 -f all -f idioms example.py
6. Ví dụ
$ 2to3 -f all -f idioms example.py
7 được chuyển đổi thành
$ 2to3 -f all -f idioms example.py
8.

________ 49¶

Thay thế tên phương thức

$ 2to3 -n -W --add-suffix=3 example.py
0 không dùng với các phương thức chính xác.

Từ

Đến

$ 2to3 -n -W --add-suffix=3 example.py
1

$ 2to3 -n -W --add-suffix=3 example.py
2

$ 2to3 -n -W --add-suffix=3 example.py
3

$ 2to3 -n -W --add-suffix=3 example.py
2

$ 2to3 -n -W --add-suffix=3 example.py
5

$ 2to3 -n -W --add-suffix=3 example.py
6

$ 2to3 -n -W --add-suffix=3 example.py
7

$ 2to3 -n -W --add-suffix=3 example.py
6

$ 2to3 -n -W --add-suffix=3 example.py
9

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
0

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
1

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
0

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
3

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
4

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
5

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
6

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
7

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
8

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
9

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
8

L = list(some_iterable)
L.sort()
1

L = list(some_iterable)
L.sort()
2

L = list(some_iterable)
L.sort()
3

L = list(some_iterable)
L.sort()
2

________ 75¶

Chuyển đổi

L = list(some_iterable)
L.sort()
6 thành
L = list(some_iterable)
L.sort()
7.

________ 78¶

Chuyển đổi

L = list(some_iterable)
L.sort()
9 thành
L = sorted(some_iterable)
0. Trình sửa lỗi này là tùy chọn vì API
L = sorted(some_iterable)
0 tương tự nhưng không chính xác giống như của
L = list(some_iterable)
L.sort()
9.

________ 83¶

Sửa các phương pháp lặp từ điển.

L = sorted(some_iterable)
4 được chuyển đổi thành
L = sorted(some_iterable)
5,
L = sorted(some_iterable)
6 thành
L = sorted(some_iterable)
7 và
L = sorted(some_iterable)
8 thành
L = sorted(some_iterable)
9. Tương tự, lib2to30, lib2to31 và lib2to32 được chuyển đổi tương ứng thành
L = sorted(some_iterable)
5,
L = sorted(some_iterable)
7 và
L = sorted(some_iterable)
9. Nó cũng kết thúc các cách sử dụng hiện có của
L = sorted(some_iterable)
5,
L = sorted(some_iterable)
7 và
L = sorted(some_iterable)
9 trong một cuộc gọi tới lib2to39.

________ 100¶

Chuyển đổi

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
01 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
02.

________ 103¶

Chuyển đổi câu lệnh

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
04 thành hàm
$ 2to3 -x apply example.py
0.

________ 106¶

Loại bỏ việc sử dụng

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
07. Đối số cho
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
07 được kết thúc trong các cuộc gọi đến
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
09,
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
10 và
$ 2to3 -x apply example.py
0.

________ 112¶

Thay đổi gán

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
13 để sử dụng mô -đun
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
14.

________ 115¶

Kết thúc việc sử dụng

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
16 trong cuộc gọi lib2to39.

________ 118¶

Khắc phục các thuộc tính chức năng đã được đổi tên. Ví dụ,

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
19 được chuyển đổi thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
20.

________ 121¶

Xóa các câu lệnh

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
22.

________ 123¶

Đổi tên

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
24 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
25.

________ 126¶

Thay đổi

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
27 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
28.

________ 129¶

Bộ sửa lỗi tùy chọn này thực hiện một số phép biến đổi làm cho mã Python thành ngữ hơn. Các so sánh loại như

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
30 và
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
31 được chuyển đổi thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
32.
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
33 trở thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
34. Fixer này cũng cố gắng sử dụng
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
35 ở những nơi thích hợp. Ví dụ: khối này

L = list(some_iterable)
L.sort()

được thay đổi thành

L = sorted(some_iterable)

________ 136¶

Phát hiện nhập khẩu anh chị em và chuyển đổi chúng thành nhập khẩu tương đối.

________ 137¶

Xử lý mô -đun đổi tên trong thư viện tiêu chuẩn.

________ 138¶

Xử lý các mô -đun khác đổi tên trong thư viện tiêu chuẩn. Nó tách biệt với trình sửa chữa

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
8 chỉ vì những hạn chế kỹ thuật.

________ 140¶

Chuyển đổi

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
41 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
42.

________ 143¶

Chuyển đổi

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
44 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
45.

________ 146¶

Khắc phục các loại trùng lặp trong đối số thứ hai của

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
47. Ví dụ,
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
48 được chuyển đổi thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
49 và
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
50 được chuyển đổi thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
51.

________ 152¶

Loại bỏ nhập khẩu của

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
53,
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
54 và
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
55. Nhập khẩu của
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
56 cũng được thay đổi thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
57.

________ 158¶

Thay đổi cách sử dụng

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
53,
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
54 và
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
55 thành các tương đương tích hợp của chúng.
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
56 được thay đổi thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
57.

________ 164¶

Đổi tên

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
65 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
66.

________ 167¶

Kết thúc

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
68 trong cuộc gọi lib2to39. Nó cũng thay đổi
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
70 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
71. Sử dụng
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
72 vô hiệu hóa bộ sửa lỗi này.

________ 173¶

Chuyển đổi cú pháp Metaclass cũ (

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
74 trong thân lớp) thành mới (
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
75).

________ 176¶

Khắc phục tên thuộc tính phương thức cũ. Ví dụ,

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
77 được chuyển đổi thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
78.

________ 179¶

Chuyển đổi cú pháp không bình đẳng cũ,

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
80, thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
81.

________ 182¶

Chuyển đổi việc sử dụng các phương thức Iterator từ

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
83 thành hàm
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
83. Nó cũng đổi tên các phương pháp
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
83 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
86.

________ 187¶

Đổi tên các định nghĩa của các phương thức được gọi là

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
88 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
89.

________ 190¶

Chuyển đổi chữ octal thành cú pháp mới.

________ 191¶

Chuyển đổi các cuộc gọi đến các chức năng khác nhau trong mô -đun

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
92 sang các lệnh gọi chức năng khác, nhưng tương đương. Khi cần thiết, các câu lệnh
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
93 thích hợp được thêm vào, ví dụ:
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
94. Bản đồ sau đây được thực hiện:

Từ

Đến

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
95

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
96

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
97

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
98

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
99

$ 2to3 -f imports -f has_key example.py
00

$ 2to3 -f imports -f has_key example.py
01

$ 2to3 -f imports -f has_key example.py
02

$ 2to3 -f imports -f has_key example.py
03

$ 2to3 -f imports -f has_key example.py
04

$ 2to3 -f imports -f has_key example.py
05

$ 2to3 -f imports -f has_key example.py
06

$ 2to3 -f imports -f has_key example.py
07

$ 2to3 -f imports -f has_key example.py
08

________ 209¶

Thêm dấu ngoặc đơn khi chúng được yêu cầu trong danh sách toàn diện. Ví dụ,

$ 2to3 -f imports -f has_key example.py
10 trở thành
$ 2to3 -f imports -f has_key example.py
11.

________ 212¶

Chuyển đổi câu lệnh

$ 2to3 -f imports -f has_key example.py
13 thành hàm
$ 2to3 -f imports -f has_key example.py
6.

________ 215¶

Chuyển đổi

$ 2to3 -f imports -f has_key example.py
16 thành
$ 2to3 -f imports -f has_key example.py
17 và
$ 2to3 -f imports -f has_key example.py
18 thành
$ 2to3 -f imports -f has_key example.py
19. Nếu
$ 2to3 -f imports -f has_key example.py
20 là một tuple, bản dịch sẽ không chính xác vì việc thay thế các bộ dữ liệu cho các trường hợp ngoại lệ đã bị xóa trong 3.0.

________ 221¶

Chuyển đổi

$ 2to3 -f imports -f has_key example.py
22 thành
$ 2to3 -f imports -f has_key example.py
23.

________ 224¶

Xử lý việc di chuyển của

$ 2to3 -f imports -f has_key example.py
25 đến
$ 2to3 -f imports -f has_key example.py
26.

________ 227¶

Chuyển đổi

$ 2to3 -f imports -f has_key example.py
28 thành
$ 2to3 -f imports -f has_key example.py
29.

________ 230¶

Thay đổi

$ 2to3 -f imports -f has_key example.py
31 thành
$ 2to3 -f imports -f has_key example.py
32.

________ 233¶

Thay thế Backtick repr với chức năng

$ 2to3 -f imports -f has_key example.py
34.

________ 235¶

Thay thế việc sử dụng hàm tạo

$ 2to3 -f imports -f has_key example.py
36 bằng chữ viết. Fixer này là tùy chọn.

________ 237¶

Đổi tên

$ 2to3 -f imports -f has_key example.py
38 thành
$ 2to3 -f imports -f has_key example.py
39.

________ 240¶

Thay đổi các loại

$ 2to3 -f imports -f has_key example.py
41,
$ 2to3 -f imports -f has_key example.py
42,
$ 2to3 -f imports -f has_key example.py
43 để sử dụng
$ 2to3 -f imports -f has_key example.py
44.

________ 245¶

Khắc phục sự thay đổi API trong phương thức Generator

$ 2to3 -f imports -f has_key example.py
46.

________ 247¶

Loại bỏ tham số Tuple Inspicting Giải nén. Bộ sửa lỗi này chèn các biến tạm thời.

________ 248¶

Sửa mã bị hỏng từ việc loại bỏ một số thành viên trong mô -đun

$ 2to3 -f imports -f has_key example.py
49.

________ 250¶

Đổi tên

$ 2to3 -f imports -f has_key example.py
51 thành
L = list(some_iterable)
L.sort()
7.

________ 253¶

Xử lý đổi tên của

$ 2to3 -f imports -f has_key example.py
54 và
$ 2to3 -f imports -f has_key example.py
55 cho gói
$ 2to3 -f imports -f has_key example.py
54.

________ 257¶

Loại bỏ khoảng trắng dư thừa khỏi các vật phẩm phân tách bằng dấu phẩy. Fixer này là tùy chọn.

________ 258¶

Đổi tên

$ 2to3 -f imports -f has_key example.py
59 thành
$ 2to3 -f imports -f has_key example.py
60 và kết thúc các cuộc gọi
$ 2to3 -f imports -f has_key example.py
60 hiện có với lib2to39.

________ 263¶

Thay đổi

$ 2to3 -f imports -f has_key example.py
64 thành
$ 2to3 -f imports -f has_key example.py
65.

________ 266¶

Kết thúc việc sử dụng

$ 2to3 -f imports -f has_key example.py
67 trong cuộc gọi lib2to39. Điều này bị vô hiệu hóa khi
$ 2to3 -f imports -f has_key example.py
69 xuất hiện.

lib2to3 - Thư viện 2to3

Mã nguồn: lib/lib2to3/ Lib/lib2to3/


Không dùng nữa kể từ phiên bản 3.11, sẽ bị xóa trong phiên bản 3.13: Python 3.9 đã chuyển sang trình phân tích cú pháp PEG (xem PEP 617) trong khi Lib2To3 đang sử dụng trình phân tích cú pháp LL (1) kém linh hoạt hơn. Python 3.10 bao gồm cú pháp ngôn ngữ mới không thể phân tích được bởi trình phân tích cú pháp LIB2TO3 LL (1) (xem PEP 634). Mô -đun lib2to3 đã được đánh dấu chờ xử lý trong Python 3.9 (tăng

$ 2to3 -f imports -f has_key example.py
72 khi nhập) và không được phản đối trong Python 3.11 (tăng
$ 2to3 -f imports -f has_key example.py
73). Nó sẽ được xóa khỏi thư viện tiêu chuẩn trong Python 3.13. Hãy xem xét các lựa chọn thay thế của bên thứ ba như Libcst hoặc PARSO.Python 3.9 switched to a PEG parser (see PEP 617) while lib2to3 is using a less flexible LL(1) parser. Python 3.10 includes new language syntax that is not parsable by lib2to3’s LL(1) parser (see PEP 634). The lib2to3 module was marked pending for deprecation in Python 3.9 (raising
$ 2to3 -f imports -f has_key example.py
72 on import) and fully deprecated in Python 3.11 (raising
$ 2to3 -f imports -f has_key example.py
73). It will be removed from the standard library in Python 3.13. Consider third-party alternatives such as LibCST or parso.

Ghi chú

API lib2to3 nên được coi là không ổn định và có thể thay đổi mạnh mẽ trong tương lai.