有人可以解释一下member_descriptor是什么以及如何从 Python 解释器访问/修改其内容吗?
Python版本=3.6
In [1]: import _pickle
In [2]: _pickle.Pickler.dispatch_table
Out[2]: <member 'dispatch_table' of '_pickle.Pickler' objects>
In [3]: type(_pickle.Pickler.dispatch_table)
Out[3]: member_descriptor
Run Code Online (Sandbox Code Playgroud)
我只能找到这个来解释member_descriptor。
我正在使用Cython的编译器指令(http://docs.cython.org/en/latest/src/reference/compilation.html#globally)。
$ cat temp.pyx
# cython: language_level=3
print("abc", "def", sep=" ,") # invalid in python 2
Run Code Online (Sandbox Code Playgroud)
编译:
$ cythonize -i world_dep.pyx
Error compiling Cython file:
------------------------------------------------------------
...
# cython: language_level=3
print("abc", "def", sep=" ,") ^
------------------------------------------------------------
temp.pyx:4:23: Expected ')', found '='
Run Code Online (Sandbox Code Playgroud)
因此,language_level指令未得到尊重。因此,cythonize最终使用Python 2语义,并且由于上述print语句在Python 2中无效而引发错误。
但是,包括任何Python语句都可以完成以下工作:
$ cat temp.pyx
# cython: language_level=3
import os
print("abc", "def", sep=" ,")
Run Code Online (Sandbox Code Playgroud)
编译和执行:
$ cythonize -i temp.pyx; python -c "import temp"
abc, def
Run Code Online (Sandbox Code Playgroud)
知道import语句如何使language_level得到尊重吗?
我也在Cython GitHub存储库上提出了同样的问题?
Python 2 文档说:
2.3 版后已弃用:使用 function(*args, **keywords) 而不是 apply(function, args, keyword)(请参阅解包参数列表)。
Pickle模块需要以下语法来定义__reduce__转储对象的方法:
def __reduce__():
return (<A callable object>, <A tuple of arguments for the callable object.>)
Run Code Online (Sandbox Code Playgroud)
(我知道从返回的元组长度__reduce__可以 >2,但需要 <=5。在当前问题的上下文中考虑长度 2 的情况。)
这意味着无法将关键字参数传递给可调用对象。在 Python 2 中,我有以下解决方法:
def __reduce__():
return (apply, (<A callable object>, ((<args>,), <kwargs>))
Run Code Online (Sandbox Code Playgroud)
但是,builtins.apply已在Python 3 中删除。builtins.apply在Python 3 中实现我自己的版本还有其他选择吗?
访问classmethod一个类总是返回一个不同的对象。这与实例或静态方法不同。
class Foo(object):
@classmethod
def foo_classmethod(cls):
pass
def foo_instancemethod(self):
pass
@staticmethod
def foo_staticmethod():
pass
Run Code Online (Sandbox Code Playgroud)
当试图比较
In [37]: print(Foo.foo_instancemethod is Foo.foo_instancemethod)
True
In [38]: print(Foo.foo_staticmethod is Foo.foo_staticmethod)
True
In [39]: print(Foo.foo_classmethod is Foo.foo_classmethod)
False
Run Code Online (Sandbox Code Playgroud)
Python 2和3中的行为相同。这看起来像是bug吗?我在酸洗Python3中的类方法并对未酸洗的is对象进行检查时遇到了这个问题。
来自https://github.com/uqfoundation/dill/blob/master/dill/dill.py#L43:
from pickle import _Pickler as StockPickler, Unpickler as StockUnpickler
这意味着 dill 继承自pickle._Pickler,它是一个纯 Python实现。然而,_pickle.Pickler是 pickle 的替代实现,并且是一个更快的版本。为什么莳萝不从它延伸出来?
pickle ×4
python ×3
python-2.7 ×3
cpython ×2
python-3.6 ×2
python-3.x ×2
class-method ×1
cython ×1
cythonize ×1
dill ×1
python-3.7 ×1