Write a decorator to add a ‘$’ sign to a number.

This program illustrates how to write decorator function to add a '$' sign to a number.

Python Source Code: Decorator for Adding Dollar Sign


# Decorator function
def decorator(fn):
    def add_dollar(*args, **kwargs):
        return '$' + str(fn(*args,**kwargs))
    return add_dollar

# Decorator in use
@decorator
def add_symbol(number):
    return number

print(add_symbol(200))
print(add_symbol(3000))
print(add_symbol(80000))

Output

$200
$3000
$80000