Python类层次结构中的动态参数在args之前。

huangapple go评论86阅读模式
英文:

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&#39;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)
        ....
    ....

huangapple
  • 本文由 发表于 2023年6月5日 21:54:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定