将修改后的 *args 传递给 python 中的基础 class

Passing a modified *args to base class in python

假设你有

class Block:
  def __init__(self, *args):   
    ... 

你可以调用 Block("resource", "some", { "other": "args})

然后你想做一个

class Resource:
    def __init__(self, *args):    
        <SOMETHING HERE calling Block passing "resource" as the first arg>

这样你就可以写

Resource("some", { "other": "args})

并让它做与

相同的事情

Block("resource", "some", { "other": "args}

<SOMETHING HERE ...> 行是什么样子的?

听起来你在这里有一个 "is a" 关系,它是通过继承正确建模的。我会推荐两种方法中的一种。

  1. 如果一个Resource是一个Block,那么就这样称呼它
class Resource(Block):
    def __init__(self, *args):
        super().__init__("resource", *args)
  1. 如果 Resource 不是 Block,则为 Block
  2. 创建一个备用构造函数
class Block:
    def __init__(self, *args):
        # instantiate however

    @classmethod
    def make_resource(cls, *args):
        return cls('resource', *args)