Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
a6216749
Kaydet (Commit)
a6216749
authored
Ock 25, 2008
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Add support for copy, deepcopy, and pickle.
üst
909e334e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
0 deletions
+23
-0
rational.py
Lib/rational.py
+15
-0
test_rational.py
Lib/test/test_rational.py
+8
-0
No files found.
Lib/rational.py
Dosyayı görüntüle @
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
Dosyayı görüntüle @
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