mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Doc: Do not encourage using a base class name in a derived class (GH-22177)
This commit is contained in:
parent
c0f22fb8b3
commit
4642ccd1c3
1 changed files with 6 additions and 6 deletions
|
@ -1517,17 +1517,17 @@ order` (MRO) with ``type(self).__mro__``, and return the next in line after
|
||||||
How can I organize my code to make it easier to change the base class?
|
How can I organize my code to make it easier to change the base class?
|
||||||
----------------------------------------------------------------------
|
----------------------------------------------------------------------
|
||||||
|
|
||||||
You could define an alias for the base class, assign the real base class to it
|
You could assign the base class to an alias and derive from the alias. Then all
|
||||||
before your class definition, and use the alias throughout your class. Then all
|
|
||||||
you have to change is the value assigned to the alias. Incidentally, this trick
|
you have to change is the value assigned to the alias. Incidentally, this trick
|
||||||
is also handy if you want to decide dynamically (e.g. depending on availability
|
is also handy if you want to decide dynamically (e.g. depending on availability
|
||||||
of resources) which base class to use. Example::
|
of resources) which base class to use. Example::
|
||||||
|
|
||||||
BaseAlias = <real base class>
|
class Base:
|
||||||
|
...
|
||||||
|
|
||||||
|
BaseAlias = Base
|
||||||
|
|
||||||
class Derived(BaseAlias):
|
class Derived(BaseAlias):
|
||||||
def meth(self):
|
|
||||||
BaseAlias.meth(self)
|
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue