Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Python question.


  • Please log in to reply
4 replies to this topic

#1 techgnostic

techgnostic

  • Members
  • 12 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:10:56 AM

Posted 04 May 2015 - 03:13 AM

I was hoping maybe somebody could explain this to me, because I'm not understanding it. 

The goal is to make the second variable (it's being called a function here, but it is effectively a variable right now as far as I can tell) equal three. Here is the correct response.

def one_good_turn(n):
    return n + 1
    
def deserves_another(m):
    return one_good_turn(m) + 2

Here's what I kept getting.

def one_good_turn(n):
     return n + 1

def deserves_another(m):
     return one_good_turn + 2

I don't understand the extra "m" in the "deserves_another" at all. The way I'm viewing it is thus...

one_good_turn = n + 1
deserves_another = (n + 1) + 2

You insert the first function (variable!) and add two. Why is there an extra "m"?



BC AdBot (Login to Remove)

 


#2 AndroidOS

AndroidOS

    Malware Search++ developer


  • Security Developer
  • 146 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:UK
  • Local time:04:56 PM

Posted 04 May 2015 - 03:31 AM

Hi there technogstic, and welcome to Bleeping Computer. :)
 
The fact is that they are functions, and not variables. So lines like this:
 

def deserves_another(m):
    return one_good_turn + 2

 
Will not work as one_good_turn is not a variable (and it needs to be called to be ran).
 
So, lets look at the following code:
 

def one_good_turn(n):
    return n + 1
    
def deserves_another(m):
    return one_good_turn(m) + 2

print(deserves_another(1))

 
We have the two functions defined as before, then we call the deserves_another() function, passing the number 1 into it (and the print the result out). The output is 3, but the important part to understand is what it actually does.

 

When the number 1 gets passed to deserves_another function, the first thing it immediately does is goes into the one_good_turn function (this is what the

one_good_turn(m)
bit does). So, in the one_good_turn function, we add one to the number. This number then gets delivered back to the deserves_another function, which adds one to the number and delivers it back to the main program.

 

Does that help you understand at all, or have I waffled on for too long? :P



#3 techgnostic

techgnostic
  • Topic Starter

  • Members
  • 12 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:10:56 AM

Posted 04 May 2015 - 03:44 AM

Thank you for the welcome and for the reply (:

 

I'm afraid I may be a bit dense, though.

 

You say that it is a function, so it has to be called to be ran.  According to this code, you call it by typing it.  Which is exactly how you access a variable.  I understand that with the functions you have the parenthesis to insert data into them (the "n" in the first part, the "m" in the second), but other than that they seem exactly the same.

 

But my main problem is that I still don't understand how "one_good_turn(m) + 2" does anything different than "one_good_turn +2".  If anything, it seems worse.  Following how everything works, this is what I'm seeing.

def one_good_turn(n):
    return n + 1
    
def deserves_another(m):
    return one_good_turn(m) + 2

print(deserves_another(1))

#First thing that happens is your variable is entered at the end of the code.
#Second thing is that the variable is plugged into the second function so we have...
#
#def deserves_another(1):
#     return one_good_...
#NOW I SEE IT!

Okay, now I see it!  I kept relating the (m) only to the second function.  But where the (m) exists at the end of the first function as within the second function isn't a reference to the second function, but rather filling the variable the first function needs.  NOW I SEE!

 

My explanation of my understanding might not have been clear, but I do understand now.  Thank you for the help (:

 

Edit: Ignore the first part of my post, because I understand that now too.  Thank you so much for the help!


Edited by techgnostic, 04 May 2015 - 03:45 AM.


#4 AndroidOS

AndroidOS

    Malware Search++ developer


  • Security Developer
  • 146 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:UK
  • Local time:04:56 PM

Posted 04 May 2015 - 04:06 AM

No problem, I'm glad you understand it. :) If there is anything else you need help with, feel free to ask.



#5 techgnostic

techgnostic
  • Topic Starter

  • Members
  • 12 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:10:56 AM

Posted 04 May 2015 - 04:10 AM

I do have a post about networking in the networking section, if you know anything about that.

http://www.bleepingcomputer.com/forums/t/575196/packetsframes-through-osi/#entry3697581 (:






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users