how to print the default value if argument is None in python

Does this work for you?

def f(name):
    print(name or 'Hello Guest')

def A(name=None):    
    f(name)

A()
Out: "Hello Guest"

A("Hello World")
Out: "Hello World"

If the name variable is being used multiple times in the function, you could just reassign it in the beginning of the function. name = name or "Hello Guest"


The best way to do this will be to use a shared default:

DEFAULT_NAME = "Hello Guest"

def f(name=DEFAULT_NAME):
    print(name)

def A(name=DEFAULT_NAME):
    f(name)

Tags:

Python