Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
a6216749
Commit
a6216749
authored
Jan 25, 2008
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for copy, deepcopy, and pickle.
parent
909e334e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
0 deletions
+23
-0
Lib/rational.py
Lib/rational.py
+15
-0
Lib/test/test_rational.py
Lib/test/test_rational.py
+8
-0
No files found.
Lib/rational.py
View file @
a6216749
...
...
@@ -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
)
Lib/test/test_rational.py
View file @
a6216749
...
...
@@ -6,6 +6,8 @@ import math
import
operator
import
rational
import
unittest
from
copy
import
copy
,
deepcopy
from
cPickle
import
dumps
,
loads
R
=
rational
.
Rational
def
_components
(
r
):
...
...
@@ -359,6 +361,12 @@ class RationalTest(unittest.TestCase):
s
+=
num
/
fact
*
sign
self
.
assertAlmostEquals
(
math
.
cos
(
1
),
s
)
def
test_copy_deepcopy_pickle
(
self
):
r
=
R
(
13
,
7
)
self
.
assertEqual
(
r
,
loads
(
dumps
(
r
)))
self
.
assertEqual
(
id
(
r
),
id
(
copy
(
r
)))
self
.
assertEqual
(
id
(
r
),
id
(
deepcopy
(
r
)))
def
test_main
():
run_unittest
(
RationalTest
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment