使用变量作为参数

时间:2019-05-14 18:56:19

标签: 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(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))

很抱歉,如果有人问到它,但我试图四处张望,找不到解决方案。 预先感谢。

1 个答案:

答案 0 :(得分:4)

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

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