0

I've been working on a python question in preparation for an exam coming up. After searching on the web in attempts to find similar functions/documentation to clear things up, I've reached a dead end. The code is walk through requiring us to determine the exact output by hand.I'm finding it really hard to interpret what the function is doing exactly, but have actually ran the script to get a closer look to see if that would clear things up. I was able to determine why some results were the way they were.

#!/usr/bin/python 
y=5 
def fun(i, x=[3,4,5], y=3): 
    print i, x,
    y += 2 
    if y != 5: 
        print y,
    print
    return y 
a = ["a", "b", "c", "d", "e", "f"] 
a[1] = 'x' 
fun(0)
fun(1, a) 
a.append("x") 
fun(2, a, 1) 
fun(3, a[2:6]) 
fun(4, 4) 
fun(5, a[:4]) 
fun(6, a[-3:-1]) 
s = "go python!" 
fun(7, s) 
fun(8, s[3:4]) 
fun(9, s[6]) 
fun(10, s) 
fun(11, s[len(s)-1]) 
fun(12, s.split(" ")[1]) 
h = { "yes":2, 4:"no", 'maybe':5} 
h['sortof'] = "6" 
fun(13, h) 
fun(14, h.keys()) 
h[0] = 4 
h['never'] = 7 
fun(15, h) 
del( h[4] ) 
fun(16, h) 
stooges = [ ["l", "c", "m"], 6, {'c':'d', 'e':'f'}, [ "x", "y"]] 
fun(17, stooges) 
fun(18, stooges[1]) 
fun(19, stooges[3][1]) 
print "20", fun(14, stooges[0][0], 7)

The output is below.

0 [3, 4, 5] 
1 ['a', 'x', 'c', 'd', 'e', 'f'] 
2 ['a', 'x', 'c', 'd', 'e', 'f', 'x'] 3 
3 ['c', 'd', 'e', 'f'] 
4 4 
5 ['a', 'x', 'c', 'd'] 
6 ['e', 'f'] 
7 go python! 
8 p 
9 h 
10 go python! 
11 ! 
12 python! 
13 {'maybe': 5, 'yes': 2, 4: 'no', 'sortof': '6'} 
14 ['maybe', 'yes', 4, 'sortof'] 
15 {0: 4, 4: 'no', 'maybe': 5, 'never': 7, 'sortof': '6', 'yes': 2} 
16 {0: 4, 'maybe': 5, 'never': 7, 'sortof': '6', 'yes': 2} 
17 [['l', 'c', 'm'], 6, {'c': 'd', 'e': 'f'}, ['x', 'y']] 
18 6 
19 y 
20 14 l 9 
9 

When fun(0) is called, i understand that 0 is simply passed into the i in the function, which prints i and x's values, resulting in 0 [3,4,5]. I also noticed that for fun(1, a), it once again passed on the 1 to the function as i and printed 1, but this time instead of printing x, it printed the value of a, which is a mutable list that was altered with a[1]='x'...Basically to get to the point, I'm not sure I understand exactly what is going on in the function, although studying the output has made it make a bit more sense, I really want to go into that exam prepared for a question like this which will be on it for sure. As of now, my confidence in doing well simply because of a question like this is pretty low since it's going to be worth a lot of marks. Really hope someone can help me out and explain what that function is doing. We've done our share of programming this semester, but nothing impractical like that. Thanks in advance.

Stryk9
  • 39
  • 1
  • 6
  • "Help me understand this code" is not a specific programming problem. – tzaman Dec 09 '14 at 00:54
  • Well to be more specific, I'm not sure how "fun(2, a, 1)" outputs "2 ['a', 'x', 'c', 'd', 'e', 'f', 'x'] 3" . I've made the assumption that the first parameter in fun() will always be i, and that if a second parameter is stated, replaces x? but then the 3rd parameter in the above example, the one outputs a 3... Sorry about that, I just really don't know where to turn to get a grasp on this. I've done numerous assignments and labs for this class, but never something like this. Googling python functions and reading about the parameters didn't help either. – Stryk9 Dec 09 '14 at 01:02
  • 2
    Read up on default parameters. The `x` will be `[3,4,5]` unless you pass in a replacement; similarly, `y` will be `3` unless you pass in a third parameter. The function adds `2` to `y` and then prints it if the value is anything other than `5` -- i.e., if you don't pass in a replacement value, it just does `3+2=5` and then doesn't print it. When you pass in a `1`, `y` becomes `1+2 (=3)` and thus it gets printed. – tzaman Dec 09 '14 at 01:16
  • 2
    http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument is also of relevance here. – tzaman Dec 09 '14 at 01:18
  • Thanks so much for this! Makes much more sense now. Great to hear it from someone than to try to look at it and make assumptions as to why it's doing what it does... I guess I was right in some cases. like fun(1, a) , 1 is now i, and x now contains a's list (replacement values?), and thus is printed. – Stryk9 Dec 09 '14 at 01:22
  • Typing it out now step by step with descriptions to what the function is doing. Really appreciate this! We like to consider these questions on the tests the "fail" ones... Seems so mind boggling, not so much anymore after you've helped me out. It's just hard to imagine us programming assignments involving webservers, cgi, mysql, and then being thrown some crap like that on a test. Haha – Stryk9 Dec 09 '14 at 02:24

1 Answers1

1

There's a lot of stuff happening in your code snippet. So to keep the answer consise (and to help you learn better) I would suggest using the Python debugger. Using the debugger is key in your path towards professional development. The debugger will let you step through the code line by line, inspect all the variables at every step of the way. This will help you understand how the variables are affected by every statement. I would really suggest going through this exercise using the debugger.

To start you off on this:

import pdb # This is the Python debugger module. Comes with Python
y=5 
def fun(i, x=[3,4,5], y=3): 
    print i, x,
    y += 2 
    if y != 5: 
        print y,
    print
    return y 

pdb.set_trace()

a = ["a", "b", "c", "d", "e", "f"] 
a[1] = 'x' 
fun(0)
fun(1, a) 
a.append("x") 
fun(2, a, 1) 

When you run this program now, the debugger will kick in a wait for your input before executing a = ["a", "b", "c", "d", "e", "f"]. If you type in "s" and enter, the debugger will execute the next statement. At anytime, just use the print command to see the value of any variable. Egs. print a will show you the value of a at that step. See the pdb docs for more options.

Hope this makes a difference somewhere in your career.

kartikg3
  • 2,590
  • 1
  • 16
  • 23
  • Very much appreciated. You guys are awesome. Have learned a lot on stackoverflow; powerful tools like regex101.com and now this. Thank you – Stryk9 Dec 09 '14 at 05:49