Python 使用变量作为参数

Python 使用变量作为参数,python,pygame,Python,Pygame,我想知道在这种情况下是否可以使用变量作为参数 这是我的代码: def text_get_position(x, y, position): if position == "midleft": text_position = text_to_screen.get_rect(midleft=(x, y)) elif position == "midright": text_position = text_to_screen.get_rect(midr

我想知道在这种情况下是否可以使用变量作为参数

这是我的代码:

def text_get_position(x, y, position):

    if position == "midleft":
        text_position = text_to_screen.get_rect(midleft=(x, y))
    elif position == "midright":
        text_position = text_to_screen.get_rect(midright=(x, y))
    else:
        text_position = text_to_screen.get_rect(center=(x, y))
我想做一些类似的事情:

def text_get_position(x, y, position):
        text_position = text_to_screen.get_rect(position=(x, y))
如果已经有人问我,我很抱歉,但我试着四处看看,却找不到解决办法。
提前感谢。

是的,您可以将命名的(“关键字”)参数作为字典传递。注意字典前面的
**

def text_get_position(x, y, position):
    text_position = text_to_screen.get_rect(**{position: (x, y)})

非常感谢,这正是我想要的。