Python function argument scope (Dictionaries v. Strings)

Posted by Shaun Meyer on Stack Overflow See other posts from Stack Overflow or by Shaun Meyer
Published on 2010-06-01T15:37:50Z Indexed on 2010/06/01 15:43 UTC
Read the original article Hit count: 233

Filed under:

Hello, given:

foo = "foo"
def bar(foo):
    foo = "bar"

bar(foo)
print foo

# foo is still "foo"...

foo = {'foo':"foo"}
def bar(foo): 
    foo['foo'] = "bar"

bar(foo)
print foo['foo']

# foo['foo'] is now "bar"?

I have a function that has been inadvertently over-writing my function parameters when I pass a dictionary. Is there a clean way to declare my parameters as constant or am I stuck making a copy of the dictionary within the function?

Thanks!

© Stack Overflow or respective owner

Related posts about python