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
6a137e8d
Kaydet (Commit)
6a137e8d
authored
Eyl 07, 2016
tarafından
Ethan Furman
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
add recipes for pseudo-valueless enums
üst
eaa3f5f1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
11 deletions
+69
-11
enum.rst
Doc/library/enum.rst
+68
-11
ACKS
Misc/ACKS
+1
-0
No files found.
Doc/library/enum.rst
Dosyayı görüntüle @
6a137e8d
...
...
@@ -694,12 +694,66 @@ are recipes for some different types of enumerations that can be used directly,
or as examples for creating one's own.
AutoNumber
^^^^^^^^^^
Omitting values
^^^^^^^^^^
^^^^^
Avoids having to specify the value for each enumeration member::
In many use-cases one doesn't care what the actual value of an enumeration
is. There are several ways to define this type of simple enumeration:
>>> class AutoNumber(Enum):
- use instances of :class:`object` as the value
- use a descriptive string as the value
- use a tuple as the value and a custom :meth:`__new__` to replace the
tuple with an :class:`int` value
Using any of these methods signifies to the user that these values are not
important, and also enables one to add, remove, or reorder members without
having to renumber the remaining members.
Whichever method you choose, you should provide a :meth:`repr` that also hides
the (unimportant) value::
>>> class NoValue(Enum):
... def __repr__(self):
... return '<%s.%s>' % (self.__class__.__name__, self.name)
...
Using :class:`object`
"""""""""""""""""""""
Using :class:`object` would look like::
>>> class Color(NoValue):
... red = object()
... green = object()
... blue = object()
...
>>> Color.green
<Color.green>
Using a descriptive string
""""""""""""""""""""""""""
Using a string as the value would look like::
>>> class Color(NoValue):
... red = 'stop'
... green = 'go'
... blue = 'too fast!'
...
>>> Color.green
<Color.green>
>>> Color.green.value
'go'
Using a custom :meth:`__new__`
""""""""""""""""""""""""""""""
Using an auto-numbering :meth:`__new__` would look like::
>>> class AutoNumber(NoValue):
... def __new__(cls):
... value = len(cls.__members__) + 1
... obj = object.__new__(cls)
...
...
@@ -711,8 +765,11 @@ Avoids having to specify the value for each enumeration member::
... green = ()
... blue = ()
...
>>> Color.green.value == 2
True
>>> Color.green
<Color.green>
>>> Color.green.value
2
.. note::
...
...
@@ -853,7 +910,7 @@ Finer Points
^^^^^^^^^^^^
Supported ``__dunder__`` names
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
""""""""""""""""""""""""""""""
:attr:`__members__` is an :class:`OrderedDict` of ``member_name``:``member``
items. It is only available on the class.
...
...
@@ -864,7 +921,7 @@ all the members are created it is no longer used.
Supported ``_sunder_`` names
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
""""""""""""""""""""""""""""
- ``_name_`` -- name of the member
- ``_value_`` -- value of the member; can be set / modified in ``__new__``
...
...
@@ -896,7 +953,7 @@ and raise an error if the two do not match::
order is lost before it can be recorded.
``Enum`` member type
~~~~~~~~~~~~~~~~~~~~
""""""""""""""""""""
:class:`Enum` members are instances of an :class:`Enum` class, and even
though they are accessible as `EnumClass.member`, they should not be accessed
...
...
@@ -917,7 +974,7 @@ besides the ``Enum`` member you looking for::
Boolean value of ``Enum`` classes and members
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""""""""""""""""""""""""""""""""""""""""""""
``Enum`` members that are mixed with non-Enum types (such as
:class:`int`, :class:`str`, etc.) are evaluated according to the mixed-in
...
...
@@ -932,7 +989,7 @@ your class::
``Enum`` classes with methods
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""""""""""""""""""""""""""""
If you give your :class:`Enum` subclass extra methods, like the `Planet`_
class above, those methods will show up in a :func:`dir` of the member,
...
...
Misc/ACKS
Dosyayı görüntüle @
6a137e8d
...
...
@@ -549,6 +549,7 @@ Jonas H.
Joseph Hackman
Barry Haddow
Philipp Hagemeister
John Hagen
Paul ten Hagen
Rasmus Hahn
Peter Haight
...
...
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