Python 2 to Python 3 convert
2024-10-19 05:48:37
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 版本区别
print
input
unicode
除法运算
/
和//
异常 as
xrange
八进制字面量 & 二进制字面量
八进制数必须写成 0o777
,原来的形式 0777
不能用了;
二进制必须写成 0b111
;
新增了一个bin()函数用于将一个整数转换成二进制字串;
- 不等运算符
Python 2.x, != 和 <>
Python 3.x, !=
去掉了repr表达式``
多个模块被改名(根据PEP8)
10.数据类型 bytes 字节码类型
- 打开文件 open
- 函数式编程, map、filter 和 reduce
可迭代的对象, next()
https://www.runoob.com/python/python-2x-3x.html
refs
https://docs.python.org/2/library/2to3.html
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
最新文章
- 2-SAT
- C# 热敏打印机 Socket 网络链接 打印 图片 (二)
- python 跨语言数据交互、json、pickle(序列化)、urllib、requests(爬虫模块)、XML。
- angulaijs中的ng-upload-file与阿里云oss服务的结合,实现在浏览器端上传文件到阿里云(速度可以达到1.5M)
- centos6.5搭建lnmp环境
- 【coursera笔记】Machine Learning(Week6)
- POJ 3624
- c#模拟百度电击器方案
- PostgreSQL的 initdb 源代码分析之九
- apache重写
- A better way to learn D3 js - iLearning D3.js
- Server Job: error: String or binary data would be truncated. The statement has been terminated.
- Haxe UI框架StablexUI的使用备忘与心得(序)
- 如何让格斗游戏的横版过关(2) Cocos2d-x 2.0.4
- (转)Amoeba for MySQL 非常好用的mysql集群软件
- Xcode 下cocos-2dx 环境搭建
- vue使用v-for时vscode报错 Elements in iteration expect to have &#39;v-bind:key&#39; directives
- sqlserver2008 查看数据库自带的索引建议
- ref和引用类型传参的区别
- php大文件下载支持断点续传
热门文章
- SpringMVC Tomcat 启动时报错:java.lang.IllegalStateException: Error starting child
- SELECT ... FOR UPDATE or SELECT ... FOR SHARE Locking Reads session
- UT /SIT/ UAT
- Coded UI
- Redis,JedisPool工具类
- pythonchallenge总述
- cloudera manager server迁移
- 加快你ROS安装的一篇文章
- Codeforces Round #646 (Div. 2) 题解 (ABCDE)
- hdu 2089不要62 (数位dp)