Neithan
Neithan

Reputation: 445

Pass all arguments of a function to another function

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:

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

This works already except for the fact that there are arguments that have to be explicitly stated with the default print function, such as end (example: print('Hello, world!', end='')). How can I make my new class's print function accept arguments such as end='' and pass them to the default print?

Upvotes: 22

Views: 33680

Answers (5)

CheradenineZK
CheradenineZK

Reputation: 318

I know it looks a bit ugly but works perfectly, if you are using a lot of keyword arguments and only want to build a facade for another method:

def print(self, print_message, end='\n', sep=' ', flush=False, file=None):
    if self.condition:
        print(**{key: value for key, value in locals().items() if key != 'self'})

Although it's a lot of boilerplate, it avoids any duplication of parameter statements.

You might also look into using a decorator to make the conditional part more pythonic. But beware that the decorator checks the condition once prior to the class instantiation.

Upvotes: 15

Mark Tolonen
Mark Tolonen

Reputation: 177600

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

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

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

When calling a function with * and **, the former tuple is expanded as if the parameters were passed separately and the latter dictionary is expanded as if they were keyword parameters.

Upvotes: 41

Steely Wing
Steely Wing

Reputation: 17597

class List(list):
    def append_twice(self, *args, **kwargs):
        self.append(*args, **kwargs)
        self.append(*args, **kwargs)
l = List()
l.append_twice("Hello")
print(l) # ['Hello', 'Hello']

Upvotes: 2

Rami
Rami

Reputation: 81

Add at the end like this

def print(self, *args, end=''):

If the arguments are dynamic or too many:

 def print(self, *args, **kwargs):

Upvotes: 1

TigerhawkT3
TigerhawkT3

Reputation: 49318

Just duplicate the named arguments for the method signature.

def print(self, *args, end='\n', sep=' ', flush=False, file=None):
    if self.condition:
        print(*args, end=end, sep=sep, flush=flush, file=file)

Upvotes: 4

Related Questions