This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author maggyero
Recipients maggyero, rhettinger
Date 2022-03-23.17:22:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1648056165.02.0.400736412128.issue44090@roundup.psfhosted.org>
In-reply-to
Content
Apologies for the long delay.

> Do we have any meaningful examples to show that this is desired and useful?

A use case of `super()` in a `classmethod` that comes to mind is for parameterized factory methods. It is a variation of the classic factory method design pattern that lets the factory method of a creator creates *multiple* products according to a parameter identifying the product to create. Overriding the factory method lets you change or extend the products that are created, by mapping existing identifiers to different products or introducing new identifiers for new products (cf. GoF’s book *Design Patterns*, section ‘Factory Method’, subsection ‘Implementation’, paragraph 2):

```
>>> class MyCreator:
...     @classmethod
...     def make(cls, product_id):
...         if product_id == 'mine': return MyProduct(creator=cls)
...         if product_id == 'yours': return YourProduct(creator=cls)
...         if product_id == 'theirs': return TheirProduct(creator=cls)
...         raise ValueError('product_id {!r} not supported'.format(product_id))
... 
>>> class YourCreator(MyCreator):
...     @classmethod
...     def make(cls, product_id):
...         if product_id == 'mine': return YourProduct(creator=cls)
...         if product_id == 'yours': return MyProduct(creator=cls)
...         return super(YourCreator, cls).make(product_id)
... 
>>> class BaseProduct:
...     def __init__(self, creator): self._creator = creator
...     def __repr__(self):
...         return '{}(creator={})'.format(
...             type(self).__name__, self._creator.__name__)
... 
>>> class MyProduct(BaseProduct): pass
... 
>>> class YourProduct(BaseProduct): pass
... 
>>> class TheirProduct(BaseProduct): pass
... 
>>> MyCreator.make('mine')
MyProduct(creator=MyCreator)
>>> YourCreator.make('mine')
YourProduct(creator=YourCreator)
```
History
Date User Action Args
2022-03-23 17:22:45maggyerosetrecipients: + maggyero, rhettinger
2022-03-23 17:22:45maggyerosetmessageid: <1648056165.02.0.400736412128.issue44090@roundup.psfhosted.org>
2022-03-23 17:22:45maggyerolinkissue44090 messages
2022-03-23 17:22:44maggyerocreate