Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a variable as an argument

Tags:

python

pygame

I was wondering if it is possible to use a variable as an argument in this case.

This is the code that I have:

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))

I want to do something like:

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

I am sorry if it is already asked but I tried to look around and couldn't find a solution. Thanks in advance.

like image 924
programmer Avatar asked Aug 09 '26 22:08

programmer


1 Answers

Yes, you can pass the named ("keyword") arguments as a dictionary. Note the ** in front of the dictionary.

def text_get_position(x, y, position):
    text_position = text_to_screen.get_rect(**{position: (x, y)})
like image 149
DYZ Avatar answered Aug 12 '26 10:08

DYZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!