last day (15 days later) » 

2:43 PM
-2
Q: I don't understand what this function does

kuronekoSo I have this piece of code in a question in an assignment: >>> def func(a, b, c): a = 2; b[0] = 'x'; c['a'] = 'y' >>> l=1; m=[1]; n={'a':0} Now the question is what is the result of this code and why. I'm sorry if this is too entry level but can anyone please help me understand what the func ...

 
Well, as is, the first line doesn't do anything.
 
I am assuming the l,m,n are inputs for the function "func". If that's the case, the output would be equal to what's in the function: l=2 (because a = 2 in the function). m[0]='x' and n['a']='y' (instead of 0, which was the initial setting for n's "a" index)
 
@Nation a = 2 will not change l outside the function…! That's maybe the catch in this question…
 
2:43 PM
@NationWidePants I was about to say what @deceze said, that l will not be changed
 
@Haveaniceday none of the variable inputs will change the variables outside the function. what do you mean? Those are assumed inputs and they will only change within the scope. Are you placing the scope someplace other than where I am (since this wasn't written as proper python?
 
@NationWidePants after passing l through the function, it will still be 1 afterward. Test it.
 
l=1 outside the function, but l=2 inside the function (assuming we're talking about l=a, because scope)
 
@Nation There is no l inside the function, so those statements are all very confusing…
 
python def func(a, b, c): a = 2 b[0] = 'x' c['a'] = 'y' if __name__ == '__main__': l=1 m=[1] n={'a':0} func(l,m,n) in the code he posted,a most certainly does end up = 2, event though the input for a was 1
 
2:43 PM
@NationWidePants then print(l) and you get 1. Or are you saying a=2 in the func?
 
@Haveaniceday It's a question of scope. He asked what happens inside the function.
 
@NationWidePants gotcha
 
```python
def func(a, b, c):
    a = 2
    b[0] = 'x'
    c['a'] = 'y'
    print(a,b,c)
if __name__ == '__main__':
    l=1
    m=[1]
    n={'a':0}
    func(l,m,n)
```
why can't they do fixed width INSIDE the comments? why does that only apply in chat? :( so annoying
 

last day (15 days later) »