zoukankan      html  css  js  c++  java
  • 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] : '1', [3, 4] : '2'}
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: unhashable type: 'list'
    >>> dictionary = {(1, 2) : '1', (3, 4) : '2'}
    >>> dictionary
    {(1, 2): '1', (3, 4): '2'}

    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参数,这样的方法称为未绑定方法。

  • 相关阅读:
    mysql联合索引命中条件
    Shiro知识初探(更新中)
    Java中使用MongoTemplate进行分批处理数据
    Java中String时间范围比较
    使用ReentrantLock
    使用Condition
    python的坑--你知道吗?
    python基础--函数全解析(1)
    CSS基本语法及页面引用
    HTML学习汇总
  • 原文地址:https://www.cnblogs.com/lxw0109/p/differences_between_python2_and_python3.html
Copyright © 2011-2022 走看看