Use of decorator to insert code in a function

could this be done using a decorator?

def func1(a, b):
  c = a%b
  return a * b + c

def func2(a, b):
  c = a%b
  return a - b * c

def func3(a, b):
  c = a%b
  return a + b + c

instead of having to specify c = a%b in each of the functions,
i write

@deco
def func1(a, b):
  return a * b

and it returns a * b + (a%b), one way to do it is,

def deco(func):
  def wrapper(*args, **kwargs):
    c = args[0] % args[1]
    return func(*args, **kwargs) + c
  return wrapper

but I would like to access the variable ‘c’ also, how do I access it?

func1.c

gives

AttributeError: 'function' object has no attribute 'c'

plus how do I access the function variables from outside the function, like, func1.a, func1.b?

According to my personal sight, code below is less complex and shorter than “decorator-used” code.

def func1(a, b, c):
    return a * b + c

def func2(a, b, c):
    return a - b * c

def func3(a, b, c):
    return a + b + c
  
def func0(func,a,b):#Main Method
    c=a%b
    return func(a,b,c)

print( func0(func1,6,4) )#Check if it works

About Your Question:
Variables in functions are forgotten by the interpreter. Because they take values depending on the parameters you pass into them. And, if you try typing func1(3,5).c, it will also raise an error; because func1(3,5) is an integer. So, you can not directly acces to function variables.

But, you can return a tuple which includes both “c” and result. By this way, you will be able to access -of course, not directly- “c”. (Global keyword and global variables can also help you, but it’s not a real solution to your question.) Please have a look at this edited code below:

def func1(a, b, c):
    return a * b + c

def func2(a, b, c):
    return a - b * c

def func3(a, b, c):
    return a + b + c
  
def func0(func,a,b):#Main Method
    c=a%b
    return ( func(a,b,c), c )

newCalculating=func0(func1,6,4)
print("Our Numbers: a=6, b=4 \nFunction:func1")
print(f"Result: {newCalculating[0]}")
print(f"Value Of c: {newCalculating[1]}")

Note: You can do the same thing via a list or dictionary, but returning tuples is a more preferred way.