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
c2ccac7b
Kaydet (Commit)
c2ccac7b
authored
Kas 16, 2018
tarafından
Andrés Delfino
Kaydeden (comit)
INADA Naoki
Kas 16, 2018
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
bpo-33816: Remove outdated metaclass example (GH-7566)
üst
37cd982d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
2 additions
and
34 deletions
+2
-34
datamodel.rst
Doc/reference/datamodel.rst
+2
-34
No files found.
Doc/reference/datamodel.rst
Dosyayı görüntüle @
c2ccac7b
...
...
@@ -1998,46 +1998,14 @@ becomes the :attr:`~object.__dict__` attribute of the class object.
Describes the implicit ``__class__`` closure reference
Metaclass example
^^^^^^^^^^^^^^^^^
Uses for metaclasses
^^^^^^^^^^^^^^^^^
^^^
The potential uses for metaclasses are boundless. Some ideas that have been
explored include enum, logging, interface checking, automatic delegation,
automatic property creation, proxies, frameworks, and automatic resource
locking/synchronization.
Here is an example of a metaclass that uses an :class:`collections.OrderedDict`
to remember the order that class variables are defined::
class OrderedClass(type):
@classmethod
def __prepare__(metacls, name, bases, **kwds):
return collections.OrderedDict()
def __new__(cls, name, bases, namespace, **kwds):
result = type.__new__(cls, name, bases, dict(namespace))
result.members = tuple(namespace)
return result
class A(metaclass=OrderedClass):
def one(self): pass
def two(self): pass
def three(self): pass
def four(self): pass
>>> A.members
('__module__', 'one', 'two', 'three', 'four')
When the class definition for *A* gets executed, the process begins with
calling the metaclass's :meth:`__prepare__` method which returns an empty
:class:`collections.OrderedDict`. That mapping records the methods and
attributes of *A* as they are defined within the body of the class statement.
Once those definitions are executed, the ordered dictionary is fully populated
and the metaclass's :meth:`__new__` method gets invoked. That method builds
the new type and it saves the ordered dictionary keys in an attribute
called ``members``.
Customizing instance and subclass checks
----------------------------------------
...
...
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