Pass all arguments of a function to another function

前端 未结 5 1540
感动是毒
感动是毒 2021-01-04 06:37

I want to have a class that I can create subclasses of that has a print function that only prints on a particular condition.

Here\'s basically what I\'m trying to do

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-04 07:21

    The standard way to pass on all arguments is as @JohnColeman suggested in a comment:

    ClassWithPrintFunctionAndReallyBadName:
        ...
        def print(self, *args, **kwargs):
            if self.condition:
                print(*args, **kwargs)
    

    args is a tuple of the non-keyword (positional) arguments, and kwargs is a dictionary of the keyword arguments.

提交回复
热议问题