[Python-checkins] peps: Clarify what's allowed in the base classes when making a new generic class.

guido.van.rossum python-checkins at python.org
Tue Mar 22 12:08:00 EDT 2016


https://hg.python.org/peps/rev/d9e5c42c7552
changeset: 6266:d9e5c42c7552
user: Guido van Rossum <guido at python.org>
date: Tue Mar 22 08:56:19 2016 -0700
summary:
 Clarify what's allowed in the base classes when making a new generic class.
files:
 pep-0484.txt | 26 +++++++++++++++++++++++++-
 1 files changed, 25 insertions(+), 1 deletions(-)
diff --git a/pep-0484.txt b/pep-0484.txt
--- a/pep-0484.txt
+++ b/pep-0484.txt
@@ -403,15 +403,39 @@
 class Pair(Generic[T, T]): # INVALID
 ...
 
+The ``Generic[T]`` base class is redundant in simple cases where you
+subclass some other generic class and specify type variables for its
+parameters::
+
+ from typing import TypeVar, Iterator
+
+ T = TypeVar('T')
+
+ class MyIter(Iterator[T]):
+ ...
+
+That class definition is equivalent to::
+
+ class MyIter(Iterator[T], Generic[T]):
+ ...
+
 You can use multiple inheritance with ``Generic``::
 
- from typing import TypeVar, Generic, Sized
+ from typing import TypeVar, Generic, Sized, Iterable, Container, Tuple
 
 T = TypeVar('T')
 
 class LinkedList(Sized, Generic[T]):
 ...
 
+ K = TypeVar('K')
+ V = TypeVar('V')
+
+ class MyMapping(Iterable[Tuple[K, V]],
+ Container[Tuple[K, V]],
+ Generic[K, V]):
+ ...
+
 Subclassing a generic class without specifying type parameters assumes
 ``Any`` for each position. In the following example, ``MyIterable``
 is not generic but implicitly inherits from ``Iterable[Any]``::
-- 
Repository URL: https://hg.python.org/peps


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /