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

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

Python class hierarchy dynamic argument before args

问题

  1. 我有两个类
  2. ```python
  3. class A(Base):
  4. @classmethod
  5. def which_active(cls, request: HttpRequest, *variants: str):
  6. super().which_active(...)
  7. class B(Base):
  8. @classmethod
  9. def which_active(cls, *variants: str):
  10. super().which_active(...)

这两个which_active方法的逻辑非常相似,我想将其抽象到一个超类中,如下所示:

  1. class Base:
  2. @classmethod
  3. def which_active(cls, x: Any, *variants: str):
  4. if cls.is_eligible(x):
  5. do_this()
  6. cls.assign(x)
  7. ....
  8. @classmethod
  9. def is_eligible(cls, x:Any):
  10. ....
  11. @classmethod
  12. def assign(cls, x:Any)
  13. ....
  14. ....

这会导致类B出现错误,因为基类中的which_active方法的签名与超类不匹配。如何设计以在子类中使用相同的方法名称但不同的参数,并有可能省略方法的第一个参数?

  1. <details>
  2. <summary>英文:</summary>
  3. 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(...)

  1. 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)
....
....

  1. 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?
  2. </details>
  3. # 答案1
  4. **得分**: 1
  5. `request`更改为命名参数,并将其放在位置参数`*variants`之后。
  6. ```python
  7. @classmethod
  8. def which_active(cls, *variants: str, request: HttpRequest = None):
  9. if request:
  10. if cls.is_eligible(request):
  11. do_this()
  12. cls.assign(request)
英文:

Change request to be a named argument, and put it after the positional *variants argument.

  1. @classmethod
  2. def which_active(cls, *variants: str, request: HttpRequest = None):
  3. if request:
  4. if cls.is_eligible(request):
  5. do_this()
  6. cls.assign(request)

答案2

得分: 0

我能够通过使用*args来解决这个问题:

  1. 我有两个类
  2. class A(Base):
  3. @classmethod
  4. def which_active(cls, request: HttpRequest, *variants: str):
  5. super().which_active(request, *variants)
  6. class B(Base):
  7. @classmethod
  8. def which_active(cls, *variants: str):
  9. super().which_active(None, *variants)
  10. class Base:
  11. @classmethod
  12. def which_active(cls, request: HttpRequest | None, *variants: str):
  13. if cls.is_eligible(request):
  14. do_this()
  15. cls.assign(request)
  16. ....
  17. @classmethod
  18. def is_eligible(cls, request: HttpRequest | None):
  19. ....
  20. @classmethod
  21. def assign(cls, request: HttpRequest | None)
  22. ....
  23. ....
英文:

I was able to solve this by using *args:

  1. I have two classes
  2. class A(Base):
  3. @classmethod
  4. def which_active(cls, request: HttpRequest, *variants: str):
  5. super().which_active(request, *variants)
  6. class B(Base):
  7. @classmethod
  8. def which_active(cls, *variants: str):
  9. super().which_active(None, *variants)
  10. class Base:
  11. @classmethod
  12. def which_active(cls, request: HttpRequest | None, *variants: str):
  13. if cls.is_eligible(request):
  14. do_this()
  15. cls.assign(request)
  16. ....
  17. @classmethod
  18. def is_eligible(cls, request: HttpRequest | None):
  19. ....
  20. @classmethod
  21. def assign(cls, request: HttpRequest | None)
  22. ....
  23. ....

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:

确定