- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在浏览 serge pygame engine 中的一些代码我在 serge.serialize
中遇到了一些我无法理解的东西:
def __setstate__(self, state=None):
"""Initialize the object to the given state for unpickling"""
self.initial_properties = Bag()
#
# Initialize first from the defaults and then from the live state
for this_state in (self.__class__._getProperties(), state):
if this_state:
for name, value in this_state:
setattr(self, name, value)
setattr(self.initial_properties, name, value)
此方法是从 Serializable
对象中检索到的。我感到困惑的是为什么 __setstate__
中的 state
参数的默认值为 None
。为什么在 unpickling 对象时不将状态发送到 __setstate__
,这在什么情况下有用?
最佳答案
如果您的类完全依赖于 __getstate__
/__setstate__
进行 pickling,这将无用。
作为 __setstate__
上的文档解释:
Upon unpickling, if the class defines
__setstate__()
, it is called with the unpicked state… If__getstate__()
returns a false value, the__setstate__()
method will not be called upon unpicking.
所以,如果你的__getstate__
返回None
,它不会在__setstate__
时传回给你;你只是不会接到电话。
但是,请注意在 2.x version ,这对于经典类(class)而言并非如此。对于经典类,“如果一个类同时定义了 __getstate__()
和 __setstate__()
,则状态对象不必是字典,这些方法可以做它们想做的事” (事实上 ,我相信在某些情况下,任何虚假值都会变成 {}
,这并没有很好地记录,但相同的 if
语句可以处理......)
不过,这并不能解释为什么您需要默认值……如果您可以获得 None
,当然,您需要编写处理 None
的代码……但是您不需要编写根本不获取参数的代码,对吧?
但是,您可能会这样做是有原因的。
首先,请注意这个特定类的 __setstate__
比平常做的更多:它“首先从默认状态,然后从实时状态”初始化。因此,从默认值进行初始化的单元测试非常有用。
最重要的是,如果您定义自定义 __reduce__
方法和 unreducer,没有理由它必须遵循与默认 unreducer 完全相同的规则。或者当然没有理由它必须调用 __setstate__
根本——但是如果你,比如说,构建一个你希望用户子类化的基类,使你的 unreducer尽可能按照默认方式工作,因此您的用户可以覆盖 __setstate__
而不是添加他们自己的整个 __reduce__
实现。
同样,如果您在 pickle
/copyreg
之上构建您自己的序列化程序,而不是按原样使用它,您的代码不必使用与 pickle
完全相同的规则。同样,它没有理由必须使用甚至相似 规则,但这样做可能会使您的用户更容易扩展您的类。
这些是否适用于 serge
,我不知道,但它们都可以轻松应用于您可能想要构建的各种类型。
关于python - 为什么 python `__setstate__` 接受 `None` 作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21841828/
这个问题在这里已经有了答案: Why does the print function return None? (1 个回答) 关闭 6 年前。 我正在学习理解。我得到了 print(x) 部分(我
我以为我理解了 Python 中的这两个单例值,直到我看到有人在代码中使用 return l1 or l2,其中 l1 和 l2 都是链表对象,并且(s)他想如果不为 None 则返回 l1,否则返回
我希望在 IPython Notebook 中使用列表理解生成枚举字符串列表。它有效,但给了我一个我不理解的奇怪输出。 cols = [] [cols.append('Value'+str(hour)
这个问题在这里已经有了答案: Why does the expression 0 >> import dis >>> def a(): ... return None is None is N
《Learning Python 5th》第608页有示例代码: >>> list(print(x.upper(), end=' ') for x in 'spam') S P A M [None,
我对此进行了搜索并遇到了列表返回函数,但我仍然不明白。 我试图理解为什么 Print 函数到另一个函数返回以下内容: 生日快乐 生日快乐 无 无 我的代码: def happy(): prin
除非我疯了 if None not in x 和 if not None in x 是等价的。有首选版本吗?我想 None not in 更像英语,因此更像 pythonic,但 not None i
尝试绘制 k-NN 分类器的决策边界但无法这样做得到 TypeError: '(slice(None, None, None), 0)' is an invalid key h = .01 # st
我正在尝试将可变大小的序列输入 LSTM。因此我使用生成器且批量大小为 1。 我有一个嵌入的 (sequence_length,)-input-tensor,并输出 (batch_size,equen
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Is there any way to know if the value of an argument i
我正在尝试根据环境变量的返回值进行条件赋值。 self._TBLFilePath = iTBLFilePath or os.environ.get("CDO_TBLPATH") + os.enviro
我正在使用 marshmallow 2.0.0rc2 验证 HTTP 请求的输入数据,并在 HTTP 响应上将 SQLAlchemy 模型加载到 JSON。我偶然发现了两个问题: 首先,在通过 HTT
我想将我设置为 None 的变量与 is 进行比较,但它失败了。 当我使用 == 将此变量与 None 进行比较时,它起作用了。 这就是我所说的变量: print type(xml.a) -> 因为
我最近遇到了这种语法,我不知道有什么区别。 如果有人能告诉我其中的区别,我将不胜感激。 最佳答案 答案解释here . 引用: A class is free to implement compari
尝试使用 BorutaPy 进行特征选择。但出现 TypeError: '(slice(None, None, None), array([0, 1, 2, 3, 4]))' 是无效键。 from s
我见过使用 [] 的代码片段, [None] , None或 ()作为 placeholder 的形状, 那是 x = tf.placeholder(..., shape=[], ...) y = t
是否有逻辑推理可以解释为什么下面的 Ansible playbook 中的两个 debug 任务分别输出 "NONE" 和 "FALSE"并且不是两者都“NONE”? - hosts: 'all'
我有一个函数,它可以返回两个整数的元组或(None, None)的元组: (出于本问题的目的,我们假设此返回格式是执行此操作的唯一方法,并且无法更改) from typing import Tuple
问题: 如何遍历字典并从中删除 None 键或值? 这是我尝试过的: 代码: import copy def _ignore(data): copied_data = copy.deepcop
什么是简洁的 python 表达方式 if : # do a bunch of stuff once 最佳答案 为什么不简单, None not in lst 关于python - 简明地说 "
我是一名优秀的程序员,十分优秀!