简体   繁体   中英

Python function walkthrough

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM