1

I want to create a new function that doing print, only if Debug=True. This how I'm doing it in JavaScript:

function log(...args) {
    if (debug) console.log.apply(null,args)
}

or

 function log() {
        if (debug) console.log.apply(null,arguments)
    }

How I can do similar things in Python?

The question: How to pass all the arguments I'm getting in Python?

blhsing
  • 91,368
  • 6
  • 71
  • 106
Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117
  • Possible duplicate of [Difference call function with asterisk parameter and without](https://stackoverflow.com/questions/31197586/difference-call-function-with-asterisk-parameter-and-without) – jasonharper Oct 08 '18 at 04:40

2 Answers2

1

You can use variable arguments and variable keyword arguments:

# assuming debug is a global variable initialized as either True or False
def log(*args, **kwargs):
    if debug:
        print(*args, **kwargs)
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

To answer your question specifically, something like the following should help you:

def log(**kwargs):
    if kwargs.get("debug") == True:
        print "{}".format(kwargs)

In particular, you can read more about *args and **kwargs - this SO question is an excellent start

Rob Gwynn-Jones
  • 677
  • 1
  • 4
  • 14