@1:
str, repr和反引号是Python将值转为字符串的3种方法
repr创建一个字符串,它以合法的Python表达式的形式表示该字符串。
Python2还有反引号`,但Python3中没有反引号,使用repr代替了反引号

@2:
unicode对象与字符串并不是同一个类型
Python中的普通字符串在内部是以8位ASCII码形式存储的,unicode字符串存储为16位unicode字符,
Python3中的所有字符串都是Unicode字符串。

  1. Python 3.4.0 (default, Jun 19 2015, 14:18:46)
  2. [GCC 4.8.2] on linux
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> type("hello")
  5. <class 'str'>
  6. >>> type(u"hello")
  7. <class 'str'>
  1. Python 2.7.6 (default, Jun 22 2015, 18:00:18)
  2.  
  3. [GCC 4.8.2] on linux2
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> type("hello")
  6. <type 'str'>
  7. >>> type(u"hello")
  8. <type 'unicode'>

@3: range() & xrange()

Python2:
range()函数一次创建整个列表; 而xrange()函数一次只创建一个数。
Python3:
range()会被转换成xrange()风格的函数。

@4:tuple有什么用?感觉完全可以用list替代,通常确实是可以替代的,但存在以下2种情况:

1): tuple可以作为dict的key,而list不行

  1. >>> dictionary = {[1, 2] : '', [3, 4] : ''}
  2. Traceback (most recent call last):
  3. File "<stdin>", line 1, in <module>
  4. TypeError: unhashable type: 'list'
  5. >>> dictionary = {(1, 2) : '', (3, 4) : ''}
  6. >>> dictionary
  7. {(1, 2): '', (3, 4): ''}

2): 很多内建函数的返回值类型是tuple。

@5: 使用新式类的方法有两种:
1. 在文件开头添加__metaclass__ = type代码行
2. 类定义时显式继承新类object
Python3.0中已经不存在旧式类。

@6: super()只能在新式类(__metaclass__ = type/class B(object))中使用

  1. #!/usr/bin/python2.7
  2. #coding:utf-8
  3. #如果想有中文注释就必须得有上面的语句
  4.  
  5. class A(object):
  6. def __init__(self, name):
  7. self.name = name
  8. def show(self):
  9. print("In A. self.name: " + self.name)
  10.  
  11. class B(A):
  12. def __init__(self, name, sex):
  13. #A.__init__(self, name) #NOTE 1: 如果不使用基类的成员变量,此处是可以不调用基类的__init__()/super()
  14. super(B, self).__init__(name) #NOTE 2: super只能在新式类中使用. if without __metaclass__ = type/class B(object), then we got "TypeError: must be type, not classobj"
  15. self.sex = sex
  16. def show(self):
  17. print("In B. self.name: {0}, self.sex: {1}".format(self.name, self.sex))
  18.  
  19. def main():
  20. a = A("lxw")
  21. a.show()
  22. b = B("wxl", "f")
  23. b.show()
  24.  
  25. if __name__ == '__main__':
  26. main()
  27. else:
  28. print("Being imported as a module.")

@7:

  1. @4:
  2. _metaclass__ = type
  3. class A:
  4. def modify(self, happy):
  5. self.happy = happy
  6.  
  7. def show(self):
  8. print(self.happy)
  9.  
  10. def main():
  11. a = A()
  12. #a.show() #AttributeError: 'A' object has no attribute 'happy'
  13. #A.show(a) #AttributeError: 'A' object has no attribute 'happy'
  14. a.happy = False
  15. a.show() #False
  16. A.show(a) #False
  17. a.modify(True)
  18. a.show() #True
  19. A.show(a) #True
  20. A.modify(a, False)
  21. a.show() #False
  22. A.show(a) #False
  23.  
  24. if __name__ == '__main__':
  25. main()

在调用一个实例的方法时,该方法的self参数会自动绑定到该实例上(这称为绑定方法)。但如果直接调用类的方法(例如Base.__init__),那么就没有实例会被绑定,这样就可以自由地提供需要的self参数,这样的方法称为未绑定方法。

Differences between Python2 and Python3的更多相关文章

  1. python2迁移python3的问题

    ▌使用 pathlib 模块来更好地处理路径 pathlib 是 Python 3默认的用于处理数据路径的模块,它能够帮助我们避免使用大量的 os.path.joins语句: from pathlib ...

  2. python2 与 python3 urllib的互相对应关系

    urllib Python2 name Python3 nameurllib.urlopen() Deprecated. See urllib.request.urlopen() which mirr ...

  3. 在同一台电脑上同时安装Python2和Python3

    目前Python的两个版本Python2和Python3同时存在,且这两个版本同时在更新与维护. 到底是选择Python2还是选择Python3,取决于当前要使用的库.框架支持哪个版本. 例如:HTM ...

  4. python2与python3在windows下共存

    python有python2(工业版)和python3,有时候我们会希望电脑上既有python2也有python3,!假设我们已经安装好,python2和python3了, 接下来我们找到python ...

  5. Python2.7<-------->Python3.x

    版本差异 from __future__   Python2.7 Python3.x 除法 / // Unicode u''                                       ...

  6. 同时使用python2和Python3

    问题:thrift生成的是python2代码,之前使用的是Python3因此需要同时使用两个版本. 方案:将python3的可执行文件重命名为python3(默认为Python),这样使用pyhton ...

  7. python2 到 python3 转换工具 2to3

    windows系统下的使用方法: (1)将python安装包下的Tools/Scripts下面的2to3.py拷贝到需要转换文件目录中. (2)dos切换到需要转换的文件目录下,运行命令2to3.py ...

  8. windows下同时安装python2与python3

    由于python2与python3并不相互兼容,并且差别较大,所以有时需要同时安装,但在操作命令行时,怎么区别python2与python3呢? 1.下载并安装Python 2.7.9和Python ...

  9. 爬虫入门---Python2和Python3的不同

    Python强大的功能使得在写爬虫的时候显得十分的简单,但是Python2和Python3在这方面有了很多区别. 本人刚入门爬虫,所以先写一点小的不同. 以爬取韩寒的一篇博客为例子: 在Python2 ...

随机推荐

  1. 搭建hadoop集群,

    这个教程是2.4.1的 ,但是亲测对于2.6.5,是可以用的,对2.5.4应该也是支持的 1.准备Linux环境 1.0先将虚拟机的网络模式选为NAT 1.1修改主机名 vi /etc/sysconf ...

  2. centos 7安装完后出现please make your choice from '1' to e

    解决方法:输入“1”,按Enter键输入“2”,按Enter键输入“q",按Enter键输入“yes”,按Enter键

  3. servlet里面拿到common.property的属性

    ---------------------common.property文件----------------------- kongxc_wx_dinghuo_orderSendMusic=http: ...

  4. Mysql8.0.16 only_full_group_by

    [1]Mysql8.0.16 关于only_full_group_by问题 应公司业务的需求,安装了Mysql8.0.16版本,原来在Mysql5.6版本执行无恙的SQL语句: SELECT prod ...

  5. Attribute在.net编程中的应用(一)

    Attribute的基本概念 经常有朋友问,Attribute是什么?它有什么用?好像没有这个东东程序也能运行.实际上在.Net中,Attribute是一个非常重要的组成部分,为了帮助大家理解和掌握A ...

  6. .NET中的枚举用法浅析

    本文简单分析了.NET中的枚举用法.分享给大家供大家参考.具体分析如下: 我理解的枚举就是编程中约定的一个“可选值”:例如QQ的在线状态,分别有    在线,Q我吧,隐身,忙碌等等...我觉得这就是一 ...

  7. boost容器bimap简单使用

    C++标准提供了map和multi_map,把key映射到value;    但是这种映射是单向的,只能是key到value,不能反过来;    boost.bimap扩展了标准库映射型容器,提供双向 ...

  8. Response响应对象

    1.HttpServletResponse HttpServletResponse是一个定义在Servlet API中的接口,继承自ServletReponse接口,用于封装HTTP响应消息.HTTP ...

  9. MySQL数据库安装文件夹与配置文件简易说明

    1.MySQL安装文件夹 bin:存放着可执行文件 include:存放头文件 lib:存放库文件 share:存放字符集.语言等信息 2.配置文件 my.ini:MySQL软件正在使用的配置文件 m ...

  10. Jenkins publish over ssh 路劲配置问题 记录

    每次通过jenkins 实现  maven项目编辑后 自动通过 ssh发布到 服务器的功能时,对配置的路劲有疑问,特整理出来 前提:服务器路径   /home/ubuntu/aps 目标: 构建后的j ...