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
f5223740
Kaydet (Commit)
f5223740
authored
Eyl 12, 2018
tarafından
Ethan Furman
Kaydeden (comit)
Miss Islington (bot)
Eyl 12, 2018
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
bpo-33437: add __new__ vs __init__ example (GH-9145)
Improve Enum docs.
https://bugs.python.org/issue33437
üst
865c17fb
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
0 deletions
+32
-0
enum.rst
Doc/library/enum.rst
+31
-0
ACKS
Misc/ACKS
+1
-0
No files found.
Doc/library/enum.rst
Dosyayı görüntüle @
f5223740
...
@@ -736,6 +736,37 @@ Some rules:
...
@@ -736,6 +736,37 @@ Some rules:
type's :meth:`__format__`. If the :class:`Enum` class's :func:`str` or
type's :meth:`__format__`. If the :class:`Enum` class's :func:`str` or
:func:`repr` is desired, use the `!s` or `!r` format codes.
:func:`repr` is desired, use the `!s` or `!r` format codes.
When to use :meth:`__new__` vs. :meth:`__init__`
------------------------------------------------
:meth:`__new__` must be used whenever you want to customize the actual value of
the :class:`Enum` member. Any other modifications may go in either
:meth:`__new__` or :meth:`__init__`, with :meth:`__init__` being preferred.
For example, if you want to pass several items to the constructor, but only
want one of them to be the value::
>>> class Coordinate(bytes, Enum):
... """
... Coordinate with binary codes that can be indexed by the int code.
... """
... def __new__(cls, value, label, unit):
... obj = bytes.__new__(cls, [value])
... obj._value_ = value
... obj.label = label
... obj.unit = unit
... return obj
... PX = (0, 'P.X', 'km')
... PY = (1, 'P.Y', 'km')
... VX = (2, 'V.X', 'km/s')
... VY = (3, 'V.Y', 'km/s')
...
>>> print(Coordinate['PY'])
Coordinate.PY
>>> print(Coordinate(3))
Coordinate.VY
Interesting examples
Interesting examples
--------------------
--------------------
...
...
Misc/ACKS
Dosyayı görüntüle @
f5223740
...
@@ -68,6 +68,7 @@ Ammar Askar
...
@@ -68,6 +68,7 @@ Ammar Askar
Neil Aspinall
Neil Aspinall
Chris AtLee
Chris AtLee
Aymeric Augustin
Aymeric Augustin
Andres Ayala
Cathy Avery
Cathy Avery
John Aycock
John Aycock
Donovan Baarda
Donovan Baarda
...
...
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