mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00
Add support for copy, deepcopy, and pickle.
This commit is contained in:
parent
909e334e8a
commit
a6216749fb
2 changed files with 23 additions and 0 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue