Special Method Protocols

__getstate__() — serialization protocol

Select the state saved for an instance. The result may be any picklable object if restoration handles it. Use a state version for long-lived data. The default object hook (3.11+) handles instance dictionaries and slots. Never deserialize untrusted pickle data.

Formal Syntax & Structure

python syntaxpython
def __getstate__(self):

Return Value

object

Serializable state.

Working Example

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