From 0ea4113d15c6a1fb1ac2f10eebe391bf5bc67d18 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Sun, 16 Aug 2009 20:27:37 -0400 Subject: [PATCH] clarify __getnewargs__ and __setstate__ --- special-method-names.html | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/special-method-names.html b/special-method-names.html index e1d7264..6d77ecb 100644 --- a/special-method-names.html +++ b/special-method-names.html @@ -703,16 +703,18 @@ class FieldStorage: to serialize an object (new pickling protocol) pickle.dump(x, file, protocol_version) x.__reduce_ex__(protocol_version) - +* control over how an object is created during unpickling x = pickle.load(file) x.__getnewargs__() - +* to restore an object’s state after unpickling x = pickle.load(file) x.__setstate__() +

* To recreate a serialized object, Python needs to create a new object that looks like the serialized object, then set the values of all the attributes on the new object. The __getnewargs__() method controls how the object is created, then the __setstate__() method controls how the attribute values are restored. +

Classes That Can Be Used in a with Block

Python 3 supports the with statement, which allows you to access an object’s properties and methods without explicitly referencing the object every time. A with block defines a runtime context; you “enter” the context when you execute the with statement, and you “exit” the context after you execute the last statement in the block.