Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

python - 2to3 not resolving .sort() to sorted()

I'm attempting to convert a library from Py 2.7.x to Py 3.7.x using 2to3 on Win10. Following reference from here.

I've seen that to convert some parts of Python you need to explicitly add Fixers, specifically:

idioms

This optional fixer performs several transformations that make Python code more idiomatic. Type comparisons like type(x) is SomeClass and type(x) == SomeClass are converted to isinstance(x, SomeClass). while 1 becomes while True. This fixer also tries to make use of sorted() in appropriate places. For example, this block

L = list(some_iterable) L.sort() is changed to

L = sorted(some_iterable)

So I add it to my commandline:

2to3 --output-dir=C:mypy3module -f all -f buffer -f idioms -f set_literal -f ws_comma -W -n C:mypy2module

2to3 will generate the correct files in the C:mypy3module folder but list.sort() has not been resolved to sorted(list)

What am I missing here?

question from:https://stackoverflow.com/questions/65646983/2to3-not-resolving-sort-to-sorted

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like the fixer literally only works for an

x = list(z)
x.sort()

pattern, not anything fancier. After all 2to3 doesn't do type analysis.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...