python之six用法

前言

最近在寫一個django的項目,在form的底層函數(shù)當(dāng)中看見了six的使用,就來學(xué)習(xí)一下。也希望寫的這些東西能夠更好的幫助大家學(xué)習(xí)django的一些高級用法、理解django的一些源碼。

通過寫這篇文章,也是希望幫助本人了解python2和python3之間的一些差別,也希望這篇文章能夠幫助到更多的人學(xué)習(xí)python。

對于部分既能在python2中也能在python3中使用的函數(shù),并沒有特殊說明,僅在代碼示例中進(jìn)行了相關(guān)的測試,也請大家注意一下~后續(xù)有時間會對整體進(jìn)行修改~

關(guān)于python2和python3中的區(qū)別,也可以參考如下資料:

  1. python函數(shù)方法屬性、模塊變量不同

本文中使用的python2的版本是2.7.13, python3的版本是3.6.1。由于本人能力有限,文中如有錯誤,歡迎指正。

運(yùn)行環(huán)境

six.PY2 返回一個表示當(dāng)前運(yùn)行環(huán)境是否為python2的boolean值

six.PY3 返回一個表示當(dāng)前運(yùn)行環(huán)境是否為python3的boolean值

源碼解析:

# Useful for very coarse version differentiation.
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3
PY34 = sys.version_info[0:2] >= (3, 4)

常量

six.class_types

這里主要是針對python中的old-style和new-style, new-style為type, old-style為 types.ClassType。

python2中同時有old-style和new-style,python3中只有new-style。

具體的區(qū)別可以參考What is the difference between old style and new style classes in Python?

six.integer_types

這里針對python2和python3中各自支持的int類型進(jìn)行了區(qū)分:在python2中,存在 int 和 long 兩種整數(shù)類型;在python3中,僅存在一種類型int。

six.string_types

這里針對python2和python3中各自的string類型進(jìn)行了區(qū)分:在python2中,使用的為basestring;在python3中,使用的為str。

six.text_type

這里針對python2和python3中的文本字符進(jìn)行了區(qū)分:在python2中,使用的文本字符的類型為unicode;在python3中使用的文本字符的類型為str。

具體可以參考Python2 與 Python3 的編碼對比

six.binary_type

這里針對python2和python3中的字節(jié)序列進(jìn)行了區(qū)分:在python2中,使用的字節(jié)序列的類型為str;在python3中使用的字節(jié)序列的類型為bytes。

具體可以參考Python2 與 Python3 的編碼對比

six.MAXSIZE

list、string、dict以及其他的容器所能支持的最大尺寸。

if PY3:
    string_types = str,
    integer_types = int,
    class_types = type,
    text_type = str
    binary_type = bytes

    MAXSIZE = sys.maxsize
else:
    string_types = basestring,
    integer_types = (int, long)
    class_types = (type, types.ClassType)
    text_type = unicode
    binary_type = str

    if sys.platform.startswith("java"):
        # Jython always uses 32 bits.
        MAXSIZE = int((1 << 31) - 1)
    else:
        # It's possible to have sizeof(long) != sizeof(Py_ssize_t).
        class X(object):

            def __len__(self):
                return 1 << 31
        try:
            len(X())
        except OverflowError:
            # 32-bit
            MAXSIZE = int((1 << 31) - 1)
        else:
            # 64-bit
            MAXSIZE = int((1 << 63) - 1)
        del X

對象模型兼容

six.get_unbound_function(meth)

針對python2和python3中unbound function的支持不同,在python2中存在unbound function,在python3中不存在unbound function。

if PY3:
    def get_unbound_function(unbound):
        return unbound
else:
    def get_unbound_function(unbound):
        return unbound.im_func

有關(guān)的測試代碼如下:

>>> class Foo():
...     def bar():
...             print(1)
...     def too(self):
...             print(2)
...
>>>

在python2的環(huán)境中:

>>> Foo.bar
<unbound method Foo.bar>
>>> Foo().bar
<bound method Foo.bar of <__main__.Foo instance at 0x10e8a7998>>
>>> Foo.too
<unbound method Foo.too>
>>> Foo().too
<bound method Foo.too of <__main__.Foo instance at 0x10e8a7998>>

在python3的環(huán)境中:

>>> Foo.bar
<function Foo.bar at 0x10ebe4bf8>
>>> Foo().bar
<bound method Foo.bar of <__main__.Foo object at 0x10ebeaa58>>
>>> Foo.too
<function Foo.too at 0x10ebe4c80>
>>> Foo().bar
<bound method Foo.bar of <__main__.Foo object at 0x10ebeaa58>>

使用six.get_unbound_function(meth):

在python2環(huán)境中:

>>> import six
>>> six.get_unbound_function(Foo.too)
<function too at 0x10e89faa0>
>>> six.get_unbound_function(Foo.bar)
<function bar at 0x10e89fa28>

在python3環(huán)境中:

>>> import six
>>> six.get_unbound_function(Foo.too)
<function Foo.too at 0x10a158c80>
>>> six.get_unbound_function(Foo.bar)
<function Foo.bar at 0x10a158bf8>

six.get_method_function(meth)

此方法可以在方法對象之外得到函數(shù)。在python2中使用im_func, 在python3中使用func。

if PY3:
    _meth_func = "__func__"
else:
    _meth_func = "im_func"
get_method_function = operator.attrgetter(_meth_func)

有關(guān)的測試代碼如下:

>>> class Foo():
...     def bar():
...             print(1)
...     def too(self):
...             print(2)
...
>>>

在python2環(huán)境中:

>>> import six
>>> six.get_method_function(Foo().bar)
<function bar at 0x10c8c6a28>
>>> six.get_method_function(Foo.bar)
<function bar at 0x10c8c6a28>
>>> six.get_method_function(Foo().too)
<function too at 0x10c8c6aa0>
>>> six.get_method_function(Foo.too)
<function too at 0x10c8c6aa0>

在python3環(huán)境中:

>>> import six
>>> six.get_method_function(Foo.bar)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute '__func__'
>>> six.get_method_function(Foo().bar)
<function Foo.bar at 0x1047dbbf8>
>>> six.get_method_function(Foo.too)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute '__func__'
>>> six.get_method_function(Foo().too)
<function Foo.too at 0x1047dbc80>

six.get_method_self(meth)

針對python2以及python3中的不同,返回bound method的self。其中:python2中使用im_self,python3中使用__self__。

if PY3:
    _meth_self = "__self__"
else:
    _meth_self = "im_self"
get_method_self = operator.attrgetter(_meth_self)

有關(guān)的測試代碼如下:

>>> class Foo():
...     def bar():
...             print(1)
...     def too(self):
...             print(2)
...
>>>

在python2的環(huán)境中:

>>> import six
>>> six.get_method_self(Foo.bar)
>>> a = six.get_method_self(Foo.bar)
>>> a
>>> six.get_method_self(Foo().bar)
<__main__.Foo instance at 0x10563da70>
>>> a = six.get_method_self(Foo().bar)
>>> a
<__main__.Foo instance at 0x10563dd88>
>>> six.get_method_self(Foo.too)
>>> a = six.get_method_self(Foo.too)
>>> a
>>> six.get_method_self(Foo().too)
<__main__.Foo instance at 0x10563da28>
>>> a = six.get_method_self(Foo().too)
>>> a
<__main__.Foo instance at 0x10563da70>

在python3的環(huán)境中:

>>> import six
>>> six.get_method_self(Foo.bar)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute '__self__'
>>> six.get_method_self(Foo().bar)
<__main__.Foo object at 0x1059bbb00>
>>> six.get_method_self(Foo.too)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute '__self__'
>>> six.get_method_self(Foo().too)
<__main__.Foo object at 0x1059bbb38>

six.get_function_closure(func)

針對python2和python3中的不同,返回函數(shù)當(dāng)中的閉包。其中,python2使用func_closure,python3使用 __closure__。

if PY3:
    _func_closure = "__closure__"
else:
    _func_closure = "func_closure"
    
get_function_closure = operator.attrgetter(_func_closure)

關(guān)于閉包的理解可以參考:

  1. 淺顯理解 Python 閉包
  2. 閉包的概念、形式與應(yīng)用

有關(guān)的測試代碼如下:

>>> def foo(n):
...     a = 1
...     def bar(n):
...             return a-n
...     return bar

此處需要注意的是foo函數(shù)返回的是bar函數(shù),而不是具體的值。

在python2的環(huán)境當(dāng)中:

>>> foo.__closure__
>>> foo(1)
<function bar at 0x10c25aa28>
>>> foo(1).__closure__
(<cell at 0x10c25eb40: int object at 0x7f9460d0bca8>,)
>>> foo(1).func_closure
(<cell at 0x10c25ed70: int object at 0x7f9460d0bca8>,)

在python3的環(huán)境當(dāng)中:

>>> foo.__closure__
>>> foo(1)
<function foo.<locals>.bar at 0x10c46dbf8>
>>> foo(1).__closure__
(<cell at 0x10c451198: int object at 0x10be73170>,)
>>> foo(1).func_closure
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'func_closure'

six.get_function_code(func)

針對python2和python3中獲取func中的code對象,將使用不同的方式進(jìn)行獲取。在python2中,將使用func_code;在python3中,將使用__code__。

if PY3:
    _func_code = "__code__"
else:
    _func_code = "func_code"
get_function_code = operator.attrgetter(_func_code)

如果你對其中的code object是什么比較感興趣,可以參考下面的資料:

  1. Exploring Python Code Objects

有關(guān)的測試代碼如下:

>>> def boo():
...     return 1

在python2的環(huán)境當(dāng)中:

>>> boo.func_code
<code object boo at 0x1101092b0, file "<stdin>", line 1>
>>> boo().func_code
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'func_code'

在python3的環(huán)境當(dāng)中:

>>> boo.__code__
<code object boo at 0x104d8a930, file "<stdin>", line 1>
>>> boo().__code__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute '__code__'

six.get_function_defaults(func)

針對python2和python3中的區(qū)別,獲取func中的默認(rèn)元組。在python2中,使用func_defaults;在python3中,使用__defaults__。

if PY3:
    _func_defaults = "__defaults__"
else:
    _func_defaults = "func_defaults"
get_function_defaults = operator.attrgetter(_func_defaults)

有關(guān)的測試代碼如下:

>>> def boo(a=1):
...     return a
...

在python2環(huán)境中:

>>> boo.func_defaults
(1,)
>>> boo.__defaults__
(1,)
>>> boo().func_defaults
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'func_defaults'
>>> boo().__defaults__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute '__defaults__'

在python3環(huán)境中:

>>> boo.__defaults__
(1,)
>>> boo.func_defaults
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'func_defaults'
>>> boo().__defaults__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute '__defaults__'
>>> boo().func_defaults
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'func_defaults'

six.get_function_globals(func)

獲取函數(shù)當(dāng)中的全局變量。在python2中,使用func_globals;在python3中,使用__globals__。

if PY3:
    _func_globals = "__globals__"
else:
    _func_globals = "func_globals"
get_function_globals = operator.attrgetter(_func_globals)

有關(guān)的測試代碼:

>>> def boo(a=1):
...     x = 100
...     b = x - a
...     return b

在python2環(huán)境中:

>>> boo.__globals__
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'boo': <function boo at 0x109a6e9b0>, '__doc__': None, '__package__': None}
>>> boo.func_globals
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'boo': <function boo at 0x109a6e9b0>, '__doc__': None, '__package__': None}

在python3環(huán)境中:

>>> boo.__globals__
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'boo': <function boo at 0x1029d8e18>}
>>> boo.func_globals
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'func_globals'

six.next(it) or six.advance_iterator(it)

獲取到迭代器的下一個。在python2環(huán)境中,使用it.next();在python3環(huán)境中,使用next(it)

try:
    advance_iterator = next
except NameError:
    def advance_iterator(it):
        return it.next()
next = advance_iterator

關(guān)于迭代器的內(nèi)容可以參考一下文件:

  1. 迭代器

在python2環(huán)境中:

>>> it = iter([1,2,3,4,5])
>>> while True:
...     try:
...             x = it.next()
...     except NameError:
...             print "name error"
...     except StopIteration:
...             print "end"
...             break
...
end

>>> it = iter([1,2,3,4,5])
>>> while True:
...     try:
...             x = next(it)
...     except NameError:
...             print "name error"
...     except StopIteration:
...             print "end"
...             break
...
end

在python3環(huán)境中:

>>> it = iter([1,2,3,4,5])
>>> while True:
...     try:
...             x = it.next()
...     except NameError:
...             print("name error")
...     except StopIteration:
...             print("end")
...             break
...
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
AttributeError: 'list_iterator' object has no attribute 'next'

>>> it = iter([1,2,3,4,5])
>>> while True:
...     try:
...             x = next(it)
...     except NameError:
...             print("name error")
...     except StopIteration:
...             print("end")
...             break
...
end

six.callable(obj)

該方法用來檢驗obj是否可以進(jìn)行調(diào)用。

關(guān)于python的callable屬性,請參考一下文件:

  1. python函數(shù)每日一講 - callable(object)
try:
    callable = callable
except NameError:
    def callable(obj):
        return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)

有關(guān)的測試代碼:

>>> def add(x, y):
...     return x + y
...

在python2環(huán)境中:

>>> callable(add)
True

在python 3.1環(huán)境中:

>>> callable(add)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'callable' is not defined

在python3.2之后的環(huán)境中:

>>> callable(add)
True

未完待續(xù)~~

結(jié)尾

本文是作者在自己空閑的時間寫的,如需轉(zhuǎn)載請注明出處,謝謝~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容