Assign variable with variable in function

Posted by freakazo on Stack Overflow See other posts from Stack Overflow or by freakazo
Published on 2010-05-06T22:34:32Z Indexed on 2010/05/06 22:38 UTC
Read the original article Hit count: 178

Filed under:
|

Let's say we have

def Foo(Bar=0,Song=0):  
    print(Bar)
    print(Song)

And I want to assign any one of the two parameters in the function with the variable sing and SongVal:

Sing = Song
SongVal = 2

So that it can be run like: Foo(Sing=SongVal) Where Sing would assign the Song parameter to the SongVal which is 2. The result should be printed like so:

0
2

So should I rewrite my function or is it possible to do it the way I want to? (With the code above you get an error saying Foo has no parameter Sing. Which I understand why, any way to overcome this without rewriting the function too much? Thanks in advance!

© Stack Overflow or respective owner

Related posts about python

Related posts about functions