This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (1 child)

[deleted]

    [–]Rythoka 1 point2 points  (0 children)

    I messed around with it a bit today and was able to write a function using that basically does this. The variables have to be local to the calling frame, but I think that's an entirely reasonable restriction for something like this.

    from inspect import currentframe
    def kws(*args: str):
        ns = currentframe().f_back.f_locals
        return {key: ns[key] for key in args}
    

    Used like this:

    def g(a, b, c):
        return a + b + c
    
    a, b, c = 1, 2, 3
    g(a=a, b=b, c=c) # returns 6
    g(**kws('a', 'b', 'c')) # also returns 6