英文:
Python class hierarchy dynamic argument before args
问题
我有两个类
```python
class A(Base):
@classmethod
def which_active(cls, request: HttpRequest, *variants: str):
super().which_active(...)
class B(Base):
@classmethod
def which_active(cls, *variants: str):
super().which_active(...)
这两个which_active
方法的逻辑非常相似,我想将其抽象到一个超类中,如下所示:
class Base:
@classmethod
def which_active(cls, x: Any, *variants: str):
if cls.is_eligible(x):
do_this()
cls.assign(x)
....
@classmethod
def is_eligible(cls, x:Any):
....
@classmethod
def assign(cls, x:Any)
....
....
这会导致类B出现错误,因为基类中的which_active
方法的签名与超类不匹配。如何设计以在子类中使用相同的方法名称但不同的参数,并有可能省略方法的第一个参数?
<details>
<summary>英文:</summary>
I have two classes
class A(Base):
@classmethod
def which_active(cls, request: HttpRequest, *variants: str):
super().which_active(...)
class B(Base):
@classmethod
def which_active(cls, *variants: str):
super().which_active(...)
The logic for both `which_active` methods are very similar and i want to abstract that into a super class like this:
class Base:
@classmethod
def which_active(cls, x: Any, *variants: str):
if cls.is_eligible(x):
do_this()
cls.assign(x)
....
@classmethod
def is_eligible(cls, x:Any):
....
@classmethod
def assign(cls, x:Any)
....
....
This will cause an error for class B because the signature of the `which_active` method in the base class doesn't match the superclass. How do I design this to use the same method names but different arguments with the possibility of omitting the first argument for the method in the subclasses?
</details>
# 答案1
**得分**: 1
将`request`更改为命名参数,并将其放在位置参数`*variants`之后。
```python
@classmethod
def which_active(cls, *variants: str, request: HttpRequest = None):
if request:
if cls.is_eligible(request):
do_this()
cls.assign(request)
英文:
Change request
to be a named argument, and put it after the positional *variants
argument.
@classmethod
def which_active(cls, *variants: str, request: HttpRequest = None):
if request:
if cls.is_eligible(request):
do_this()
cls.assign(request)
答案2
得分: 0
我能够通过使用*args来解决这个问题:
我有两个类
class A(Base):
@classmethod
def which_active(cls, request: HttpRequest, *variants: str):
super().which_active(request, *variants)
class B(Base):
@classmethod
def which_active(cls, *variants: str):
super().which_active(None, *variants)
class Base:
@classmethod
def which_active(cls, request: HttpRequest | None, *variants: str):
if cls.is_eligible(request):
do_this()
cls.assign(request)
....
@classmethod
def is_eligible(cls, request: HttpRequest | None):
....
@classmethod
def assign(cls, request: HttpRequest | None)
....
....
英文:
I was able to solve this by using *args:
I have two classes
class A(Base):
@classmethod
def which_active(cls, request: HttpRequest, *variants: str):
super().which_active(request, *variants)
class B(Base):
@classmethod
def which_active(cls, *variants: str):
super().which_active(None, *variants)
class Base:
@classmethod
def which_active(cls, request: HttpRequest | None, *variants: str):
if cls.is_eligible(request):
do_this()
cls.assign(request)
....
@classmethod
def is_eligible(cls, request: HttpRequest | None):
....
@classmethod
def assign(cls, request: HttpRequest | None)
....
....
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论