diff options
author | Raymond Hettinger <python@rcn.com> | 2008-01-25 00:21:54 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2008-01-25 00:21:54 (GMT) |
commit | a6216749fb88fb508cd469839d77d4264a881bd4 (patch) | |
tree | b61e404c72e88f8ad44d6a82b8739b24a26abebe /Lib/rational.py | |
parent | 909e334e8a525e8430f1532c0ecf133f19d3d185 (diff) | |
download | cpython-a6216749fb88fb508cd469839d77d4264a881bd4.zip cpython-a6216749fb88fb508cd469839d77d4264a881bd4.tar.gz cpython-a6216749fb88fb508cd469839d77d4264a881bd4.tar.bz2 |
Add support for copy, deepcopy, and pickle.
Diffstat (limited to 'Lib/rational.py')
-rwxr-xr-x | Lib/rational.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/Lib/rational.py b/Lib/rational.py index 0d3ea2f..031ee0f 100755 --- a/Lib/rational.py +++ b/Lib/rational.py @@ -490,3 +490,18 @@ class Rational(RationalAbc): def __nonzero__(a): """a != 0""" return a.numerator != 0 + + # support for pickling, copy, and deepcopy + + def __reduce__(self): + return (self.__class__, (str(self),)) + + def __copy__(self): + if type(self) == Rational: + return self # I'm immutable; therefore I am my own clone + return self.__class__(self.numerator, self.denominator) + + def __deepcopy__(self, memo): + if type(self) == Rational: + return self # My components are also immutable + return self.__class__(self.numerator, self.denominator) |