Differences between Python2 and Python3
@1:
str, repr和反引号是Python将值转为字符串的3种方法
repr创建一个字符串,它以合法的Python表达式的形式表示该字符串。
Python2还有反引号`,但Python3中没有反引号,使用repr代替了反引号
@2:
unicode对象与字符串并不是同一个类型
Python中的普通字符串在内部是以8位ASCII码形式存储的,unicode字符串存储为16位unicode字符,
Python3中的所有字符串都是Unicode字符串。
Python 3.4.0 (default, Jun 19 2015, 14:18:46)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> type("hello")
<class 'str'>
>>> type(u"hello")
<class 'str'>
Python 2.7.6 (default, Jun 22 2015, 18:00:18) [GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> type("hello")
<type 'str'>
>>> type(u"hello")
<type 'unicode'>
@3: range() & xrange()
Python2:
range()函数一次创建整个列表; 而xrange()函数一次只创建一个数。
Python3:
range()会被转换成xrange()风格的函数。
@4:tuple有什么用?感觉完全可以用list替代,通常确实是可以替代的,但存在以下2种情况:
1): tuple可以作为dict的key,而list不行
>>> dictionary = {[1, 2] : '', [3, 4] : ''}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> dictionary = {(1, 2) : '', (3, 4) : ''}
>>> dictionary
{(1, 2): '', (3, 4): ''}
2): 很多内建函数的返回值类型是tuple。
@5: 使用新式类的方法有两种:
1. 在文件开头添加__metaclass__ = type代码行
2. 类定义时显式继承新类object
Python3.0中已经不存在旧式类。
@6: super()只能在新式类(__metaclass__ = type/class B(object))中使用
#!/usr/bin/python2.7
#coding:utf-8
#如果想有中文注释就必须得有上面的语句 class A(object):
def __init__(self, name):
self.name = name
def show(self):
print("In A. self.name: " + self.name) class B(A):
def __init__(self, name, sex):
#A.__init__(self, name) #NOTE 1: 如果不使用基类的成员变量,此处是可以不调用基类的__init__()/super()
super(B, self).__init__(name) #NOTE 2: super只能在新式类中使用. if without __metaclass__ = type/class B(object), then we got "TypeError: must be type, not classobj"
self.sex = sex
def show(self):
print("In B. self.name: {0}, self.sex: {1}".format(self.name, self.sex)) def main():
a = A("lxw")
a.show()
b = B("wxl", "f")
b.show() if __name__ == '__main__':
main()
else:
print("Being imported as a module.")
@7:
@4:
_metaclass__ = type
class A:
def modify(self, happy):
self.happy = happy def show(self):
print(self.happy) def main():
a = A()
#a.show() #AttributeError: 'A' object has no attribute 'happy'
#A.show(a) #AttributeError: 'A' object has no attribute 'happy'
a.happy = False
a.show() #False
A.show(a) #False
a.modify(True)
a.show() #True
A.show(a) #True
A.modify(a, False)
a.show() #False
A.show(a) #False if __name__ == '__main__':
main()
在调用一个实例的方法时,该方法的self参数会自动绑定到该实例上(这称为绑定方法)。但如果直接调用类的方法(例如Base.__init__),那么就没有实例会被绑定,这样就可以自由地提供需要的self参数,这样的方法称为未绑定方法。
Differences between Python2 and Python3的更多相关文章
- python2迁移python3的问题
▌使用 pathlib 模块来更好地处理路径 pathlib 是 Python 3默认的用于处理数据路径的模块,它能够帮助我们避免使用大量的 os.path.joins语句: from pathlib ...
- python2 与 python3 urllib的互相对应关系
urllib Python2 name Python3 nameurllib.urlopen() Deprecated. See urllib.request.urlopen() which mirr ...
- 在同一台电脑上同时安装Python2和Python3
目前Python的两个版本Python2和Python3同时存在,且这两个版本同时在更新与维护. 到底是选择Python2还是选择Python3,取决于当前要使用的库.框架支持哪个版本. 例如:HTM ...
- python2与python3在windows下共存
python有python2(工业版)和python3,有时候我们会希望电脑上既有python2也有python3,!假设我们已经安装好,python2和python3了, 接下来我们找到python ...
- Python2.7<-------->Python3.x
版本差异 from __future__ Python2.7 Python3.x 除法 / // Unicode u'' ...
- 同时使用python2和Python3
问题:thrift生成的是python2代码,之前使用的是Python3因此需要同时使用两个版本. 方案:将python3的可执行文件重命名为python3(默认为Python),这样使用pyhton ...
- python2 到 python3 转换工具 2to3
windows系统下的使用方法: (1)将python安装包下的Tools/Scripts下面的2to3.py拷贝到需要转换文件目录中. (2)dos切换到需要转换的文件目录下,运行命令2to3.py ...
- windows下同时安装python2与python3
由于python2与python3并不相互兼容,并且差别较大,所以有时需要同时安装,但在操作命令行时,怎么区别python2与python3呢? 1.下载并安装Python 2.7.9和Python ...
- 爬虫入门---Python2和Python3的不同
Python强大的功能使得在写爬虫的时候显得十分的简单,但是Python2和Python3在这方面有了很多区别. 本人刚入门爬虫,所以先写一点小的不同. 以爬取韩寒的一篇博客为例子: 在Python2 ...
随机推荐
- 通过PDO 连接SQL Server
下载PDO_DBLIB库 PDO的各种库都可以在PECL中找到,例如,MySQL库:PDO_MYSQL.Oracle库:PDO_OCI. 作为SQL Server 的连接库,通过下面命令下载PDO_D ...
- bzoj2005 能量采集 莫比乌斯或者普通容斥
/** 题目:bzoj2005 能量采集 链接:https://vjudge.net/contest/178455#problem/F 题意:栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可 ...
- HBase学习笔记——概念及原理
1.什么是HBase HBase – Hadoop Database,是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群. ...
- Hive学习笔记——基本配置及测试
1.什么是Hive Hive 是建立在 Hadoop上的数据仓库基础构架.它提供了一系列的工具,可以用来进行数据提取转化加载(ETL),这是一种可以存储.查询和分析存储在Hadoop中的大规模数据的机 ...
- 安装Node.js以及Hexo
安装前提 安装 Hexo 相当简单.然而在安装前,您必须检查电脑中是否已安装下列应用程序: Node.js 如何在Ubuntu上安装最新版本的Node.js https://hexo.io/zh-cn ...
- Python之Matplotlib库常用函数大全(含注释)
plt.savefig(‘test’, dpi = 600) :将绘制的图画保存成png格式,命名为 test plt.ylabel(‘Grade’) : y轴的名称 plt.axis([-1, 1 ...
- Openstack(Kilo)安装系列之nova(七)
控制节点 Before you install and configure the Compute service, you must create a database, service crede ...
- requirejs学习(一)
requirejs学习(一) 随着网站功能逐渐丰富,网页中的js也变得越来越复杂和臃肿,各种依赖(插件等)也逐渐增多,原有通过script标签来导入一个个的js文件这种方式已经不能满足现在的需求,我们 ...
- 到底什么是hash
1.什么是hash算法 Hash(散列.杂凑)算法,是把任意长度的输入通过特定的算法变换成固定长度的输出,输出的值就是hash值.这个特定的算法就叫hash算法,hash算法并不是一个固定不变的算法. ...
- nvm-windows 手动安装 nvm use 无效 'node' 不是内部或外部命令,也不是可运行的程序
按照这两位前辈的教程,安装了nvm-windows. http://www.cnblogs.com/yesyes/p/7403184.html http://blog.csdn.net/jingsi1 ...