[Python] Extracting from **kwargs

Discussão em 'Python' iniciado por Stack, Junho 4, 2025.

  1. Stack

    Stack Membro Participativo

    I am writing a pretty basic calculator for functions (for example f(x)=x+5), and the first step is writing a quick function to calculate the result if we already know all the variables. Source code is:

    def calc(function, **kwargs):
    if re.fullmatch(r'[0-9a-z+\-*/()]+', function) != None:
    return eval(function)
    return None


    But I need to add a script that changes the function to add in the values instead of the variables. For example, if I call my calc() as calc('x+5*y', x=2, y=7), it should change the function to simply '2+5*7', so that I could put it into eval().

    Does anybody have any ideas on how this would be possible, or what are some alternative methods for this?

    Continue reading...

Compartilhe esta Página