前言
最近在寫一個(gè)django的項(xiàng)目压语,在form的底層函數(shù)當(dāng)中看見了six的使用格仲,就來學(xué)習(xí)一下吱晒。也希望寫的這些東西能夠更好的幫助大家學(xué)習(xí)django的一些高級用法财著、理解django的一些源碼联四。
通過寫這篇文章,也是希望幫助本人了解python2和python3之間的一些差別撑教,也希望這篇文章能夠幫助到更多的人學(xué)習(xí)python朝墩。
對于部分既能在python2中也能在python3中使用的函數(shù),并沒有特殊說明伟姐,僅在代碼示例中進(jìn)行了相關(guān)的測試收苏,也請大家注意一下~后續(xù)有時(shí)間會對整體進(jìn)行修改~
關(guān)于python2和python3中的區(qū)別,也可以參考如下資料:
本文中使用的python2的版本是2.7.13鹿霸, python3的版本是3.6.1。由于本人能力有限秆乳,文中如有錯(cuò)誤懦鼠,歡迎指正。
運(yùn)行環(huán)境
six.PY2 返回一個(gè)表示當(dāng)前運(yùn)行環(huán)境是否為python2的boolean值
six.PY3 返回一個(gè)表示當(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中同時(shí)有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)于閉包的理解可以參考:
有關(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是什么比較感興趣缎脾,可以參考下面的資料:
有關(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)
獲取到迭代器的下一個(gè)譬涡。在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)容可以參考一下文件:
在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)
該方法用來檢驗(yàn)obj是否可以進(jìn)行調(diào)用沟使。
關(guān)于python的callable屬性,請參考一下文件:
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é)尾
本文是作者在自己空閑的時(shí)間寫的渊跋,如需轉(zhuǎn)載請注明出處腊嗡,謝謝~