Python 2 to Python 3 convert

2to3, 自动将 Python 2 代码转为 Python 3 代码

https://docs.python.org/zh-cn/2/library/2to3.html

$ 2to3 example.py

# -w
$ 2to3 -w example.py $

demo

before, Python 2

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

after, Python 3

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

2to3 in action

# coding: utf8

__author__ = 'xgqfrms'
__editor__ = 'vscode'
__version__ = '1.0.1'
__copyright__ = """
Copyright (c) 2012-2050, xgqfrms; mailto:xgqfrms@xgqfrms.xyz
""" class Test(object):
def sum(a, b):
sum a + b
print "a+b=" + sum
return sum

2to3

# coding: utf8

__author__ = 'xgqfrms'
__editor__ = 'vscode'
__version__ = '1.0.1'
__copyright__ = """
Copyright (c) 2012-2050, xgqfrms; mailto:xgqfrms@xgqfrms.xyz
""" class Test(object):
def sum(a, b):
sum a + b
print("a+b=", sum)
return sum

Python 2.x 与 Python 3​​.x 版本区别

  1. print

  2. input

  3. unicode

  4. 除法运算 ///

  5. 异常 as

  6. xrange

  7. 八进制字面量 & 二进制字面量

八进制数必须写成 0o777,原来的形式 0777 不能用了;

二进制必须写成 0b111

新增了一个bin()函数用于将一个整数转换成二进制字串;

  1. 不等运算符

Python 2.x, != 和 <>

Python 3.x, !=

  1. 去掉了repr表达式``

  2. 多个模块被改名(根据PEP8)

10.数据类型 bytes 字节码类型

  1. 打开文件 open

  1. 函数式编程, map、filter 和 reduce

可迭代的对象, next()

https://www.runoob.com/python/python-2x-3x.html

refs

https://pythonconverter.com/

https://docs.python.org/2/library/2to3.html



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


最新文章

  1. 2-SAT
  2. C# 热敏打印机 Socket 网络链接 打印 图片 (二)
  3. python 跨语言数据交互、json、pickle(序列化)、urllib、requests(爬虫模块)、XML。
  4. angulaijs中的ng-upload-file与阿里云oss服务的结合,实现在浏览器端上传文件到阿里云(速度可以达到1.5M)
  5. centos6.5搭建lnmp环境
  6. 【coursera笔记】Machine Learning(Week6)
  7. POJ 3624
  8. c#模拟百度电击器方案
  9. PostgreSQL的 initdb 源代码分析之九
  10. apache重写
  11. A better way to learn D3 js - iLearning D3.js
  12. Server Job: error: String or binary data would be truncated. The statement has been terminated.
  13. Haxe UI框架StablexUI的使用备忘与心得(序)
  14. 如何让格斗游戏的横版过关(2) Cocos2d-x 2.0.4
  15. (转)Amoeba for MySQL 非常好用的mysql集群软件
  16. Xcode 下cocos-2dx 环境搭建
  17. vue使用v-for时vscode报错 Elements in iteration expect to have &#39;v-bind:key&#39; directives
  18. sqlserver2008 查看数据库自带的索引建议
  19. ref和引用类型传参的区别
  20. php大文件下载支持断点续传

热门文章

  1. SpringMVC Tomcat 启动时报错:java.lang.IllegalStateException: Error starting child
  2. SELECT ... FOR UPDATE or SELECT ... FOR SHARE Locking Reads session
  3. UT /SIT/ UAT
  4. Coded UI
  5. Redis,JedisPool工具类
  6. pythonchallenge总述
  7. cloudera manager server迁移
  8. 加快你ROS安装的一篇文章
  9. Codeforces Round #646 (Div. 2) 题解 (ABCDE)
  10. hdu 2089不要62 (数位dp)