Special Method Protocols

__setstate__() — serialization protocol

Restore deserialized instance state. Initialization is usually bypassed during unpickling; restore required invariants here. A None reduction state skips this hook. Only deserialize trusted data.

Formal Syntax & Structure

python syntaxpython
def __setstate__(self, state):

Parameters & Attributes

NameTypeDescription
stateobjectPreviously serialized state.

Return Value

None

State restored without a meaningful result.

Working Example

Example Snippetpython
import copyclass Cache:    def __init__(self): self.name="report"; self.cache=[1,2]    def __getstate__(self): return {"name":self.name}    def __setstate__(self,state): self.name=state["name"]; self.cache=[]print(copy.copy(Cache()).cache)
Python Reference