Trouble understanding the difference between passing result to another function and retuning result to...
up vote
2
down vote
favorite
I've written a script in python using two functions within it. The first function is supposed to get some links from a webpage and the other should print it in the console.
My question is what difference does it make when I pass the result from one function to another function using return
keyword like return get_info(elem)
? Usually doing only this get_info(elem)
, I can pass stuffs from one function to another then when to choose this return get_info(elem)
and why?
An example might be:
import requests
from bs4 import BeautifulSoup
def get_links(url):
response = requests.get(url)
soup = BeautifulSoup(response.text,"lxml")
elem = soup.select_one(".info h2 a[data-analytics]").get("href")
get_info(elem) #why this one
return get_info(elem) #or why this
def get_info(link):
print(link)
python python-3.x function web-scraping return
add a comment |
up vote
2
down vote
favorite
I've written a script in python using two functions within it. The first function is supposed to get some links from a webpage and the other should print it in the console.
My question is what difference does it make when I pass the result from one function to another function using return
keyword like return get_info(elem)
? Usually doing only this get_info(elem)
, I can pass stuffs from one function to another then when to choose this return get_info(elem)
and why?
An example might be:
import requests
from bs4 import BeautifulSoup
def get_links(url):
response = requests.get(url)
soup = BeautifulSoup(response.text,"lxml")
elem = soup.select_one(".info h2 a[data-analytics]").get("href")
get_info(elem) #why this one
return get_info(elem) #or why this
def get_info(link):
print(link)
python python-3.x function web-scraping return
It sounds like you might be thinking of languages like Ruby which has implicit returns. Python doesn't have those and neither do most languages.
– pguardiario
Nov 22 at 6:23
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I've written a script in python using two functions within it. The first function is supposed to get some links from a webpage and the other should print it in the console.
My question is what difference does it make when I pass the result from one function to another function using return
keyword like return get_info(elem)
? Usually doing only this get_info(elem)
, I can pass stuffs from one function to another then when to choose this return get_info(elem)
and why?
An example might be:
import requests
from bs4 import BeautifulSoup
def get_links(url):
response = requests.get(url)
soup = BeautifulSoup(response.text,"lxml")
elem = soup.select_one(".info h2 a[data-analytics]").get("href")
get_info(elem) #why this one
return get_info(elem) #or why this
def get_info(link):
print(link)
python python-3.x function web-scraping return
I've written a script in python using two functions within it. The first function is supposed to get some links from a webpage and the other should print it in the console.
My question is what difference does it make when I pass the result from one function to another function using return
keyword like return get_info(elem)
? Usually doing only this get_info(elem)
, I can pass stuffs from one function to another then when to choose this return get_info(elem)
and why?
An example might be:
import requests
from bs4 import BeautifulSoup
def get_links(url):
response = requests.get(url)
soup = BeautifulSoup(response.text,"lxml")
elem = soup.select_one(".info h2 a[data-analytics]").get("href")
get_info(elem) #why this one
return get_info(elem) #or why this
def get_info(link):
print(link)
python python-3.x function web-scraping return
python python-3.x function web-scraping return
asked Nov 22 at 5:04
robots.txt
19311
19311
It sounds like you might be thinking of languages like Ruby which has implicit returns. Python doesn't have those and neither do most languages.
– pguardiario
Nov 22 at 6:23
add a comment |
It sounds like you might be thinking of languages like Ruby which has implicit returns. Python doesn't have those and neither do most languages.
– pguardiario
Nov 22 at 6:23
It sounds like you might be thinking of languages like Ruby which has implicit returns. Python doesn't have those and neither do most languages.
– pguardiario
Nov 22 at 6:23
It sounds like you might be thinking of languages like Ruby which has implicit returns. Python doesn't have those and neither do most languages.
– pguardiario
Nov 22 at 6:23
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Let us first simplify your function so that you can run it and compare the results:
def get_links(url):
url = "this returns link: {}".format(url)
get_info(url) #why this one
return get_info(url) #or why this
def get_info(link):
print(link)
get_links('google.com')
>>this returns link: google.com
>>this returns link: google.com
Your function now returns print
twice. First when you called the function, and second when you returned the function, and in this case actually returns None
because get_info
does not return anything.
This is evident here:
url = get_links('google.com')
>>this returns link: google.com
>>this returns link: google.com
url
>> *nothing happens*
The results of return are more apparent if it actually does something, for example:
def get_links(url):
url = "this returns link: {}".format(url)
return get_info(url)
def get_info(link):
return "get_info does something, {}".format(link)
url = get_links('google.com')
url
>>'get_info does something, this returns link: google.com'
If you do not use return, it just means the function will not return anything, which happens for example if you just want to print
the results as you did. You can further try this out by assigning a name like I did above to a function does has no return and the result will essentially be None
.
I don't think he's calling the function twice. He's just asking what the difference is between calling it with or withoutreturn
.
– Barmar
Nov 22 at 5:37
I just meant to illustrate that the 2 calls inget_links
callget_info
twice, one withreturn
and one without does not return any value even when assigned to a variable because it is probably different than the way he intended it to work.
– BernardL
Nov 22 at 5:43
Read the comments in the code, it says "why this ... or this". He wants to know why he should use one or the other.
– Barmar
Nov 22 at 6:00
Perhaps. Why was too broad for me to answer and therefore I replied with an explanation of what it does.
– BernardL
Nov 22 at 6:10
If you haven't come across this post already, please take a look @BernardL.
– robots.txt
Nov 22 at 16:48
|
show 1 more comment
up vote
1
down vote
return get_info(elem)
will call the get_info()
function, then take whatever it returned, and return that same value from get_links()
. It's roughly equivalent to:
temp = get_info(elem)
return temp
But since get_info()
doesn't return anything, it just prints the link, there's not much point in using it in a return
statement. Writing just
get_info(elem)
calls the function without doing anything with its return value (if it returned anything).
I can see a huge difference between them when I go through this script. The script in the answer has got thisreturn get_data(itemlink['href'])
without which I don't get any result. I meant I don't get any result when I doget_data(itemlink['href'])
.
– robots.txt
Nov 22 at 5:16
get_data
doesn't return anything, it just prints, like theget_info
function here.
– Barmar
Nov 22 at 5:37
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424187%2ftrouble-understanding-the-difference-between-passing-result-to-another-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Let us first simplify your function so that you can run it and compare the results:
def get_links(url):
url = "this returns link: {}".format(url)
get_info(url) #why this one
return get_info(url) #or why this
def get_info(link):
print(link)
get_links('google.com')
>>this returns link: google.com
>>this returns link: google.com
Your function now returns print
twice. First when you called the function, and second when you returned the function, and in this case actually returns None
because get_info
does not return anything.
This is evident here:
url = get_links('google.com')
>>this returns link: google.com
>>this returns link: google.com
url
>> *nothing happens*
The results of return are more apparent if it actually does something, for example:
def get_links(url):
url = "this returns link: {}".format(url)
return get_info(url)
def get_info(link):
return "get_info does something, {}".format(link)
url = get_links('google.com')
url
>>'get_info does something, this returns link: google.com'
If you do not use return, it just means the function will not return anything, which happens for example if you just want to print
the results as you did. You can further try this out by assigning a name like I did above to a function does has no return and the result will essentially be None
.
I don't think he's calling the function twice. He's just asking what the difference is between calling it with or withoutreturn
.
– Barmar
Nov 22 at 5:37
I just meant to illustrate that the 2 calls inget_links
callget_info
twice, one withreturn
and one without does not return any value even when assigned to a variable because it is probably different than the way he intended it to work.
– BernardL
Nov 22 at 5:43
Read the comments in the code, it says "why this ... or this". He wants to know why he should use one or the other.
– Barmar
Nov 22 at 6:00
Perhaps. Why was too broad for me to answer and therefore I replied with an explanation of what it does.
– BernardL
Nov 22 at 6:10
If you haven't come across this post already, please take a look @BernardL.
– robots.txt
Nov 22 at 16:48
|
show 1 more comment
up vote
1
down vote
accepted
Let us first simplify your function so that you can run it and compare the results:
def get_links(url):
url = "this returns link: {}".format(url)
get_info(url) #why this one
return get_info(url) #or why this
def get_info(link):
print(link)
get_links('google.com')
>>this returns link: google.com
>>this returns link: google.com
Your function now returns print
twice. First when you called the function, and second when you returned the function, and in this case actually returns None
because get_info
does not return anything.
This is evident here:
url = get_links('google.com')
>>this returns link: google.com
>>this returns link: google.com
url
>> *nothing happens*
The results of return are more apparent if it actually does something, for example:
def get_links(url):
url = "this returns link: {}".format(url)
return get_info(url)
def get_info(link):
return "get_info does something, {}".format(link)
url = get_links('google.com')
url
>>'get_info does something, this returns link: google.com'
If you do not use return, it just means the function will not return anything, which happens for example if you just want to print
the results as you did. You can further try this out by assigning a name like I did above to a function does has no return and the result will essentially be None
.
I don't think he's calling the function twice. He's just asking what the difference is between calling it with or withoutreturn
.
– Barmar
Nov 22 at 5:37
I just meant to illustrate that the 2 calls inget_links
callget_info
twice, one withreturn
and one without does not return any value even when assigned to a variable because it is probably different than the way he intended it to work.
– BernardL
Nov 22 at 5:43
Read the comments in the code, it says "why this ... or this". He wants to know why he should use one or the other.
– Barmar
Nov 22 at 6:00
Perhaps. Why was too broad for me to answer and therefore I replied with an explanation of what it does.
– BernardL
Nov 22 at 6:10
If you haven't come across this post already, please take a look @BernardL.
– robots.txt
Nov 22 at 16:48
|
show 1 more comment
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Let us first simplify your function so that you can run it and compare the results:
def get_links(url):
url = "this returns link: {}".format(url)
get_info(url) #why this one
return get_info(url) #or why this
def get_info(link):
print(link)
get_links('google.com')
>>this returns link: google.com
>>this returns link: google.com
Your function now returns print
twice. First when you called the function, and second when you returned the function, and in this case actually returns None
because get_info
does not return anything.
This is evident here:
url = get_links('google.com')
>>this returns link: google.com
>>this returns link: google.com
url
>> *nothing happens*
The results of return are more apparent if it actually does something, for example:
def get_links(url):
url = "this returns link: {}".format(url)
return get_info(url)
def get_info(link):
return "get_info does something, {}".format(link)
url = get_links('google.com')
url
>>'get_info does something, this returns link: google.com'
If you do not use return, it just means the function will not return anything, which happens for example if you just want to print
the results as you did. You can further try this out by assigning a name like I did above to a function does has no return and the result will essentially be None
.
Let us first simplify your function so that you can run it and compare the results:
def get_links(url):
url = "this returns link: {}".format(url)
get_info(url) #why this one
return get_info(url) #or why this
def get_info(link):
print(link)
get_links('google.com')
>>this returns link: google.com
>>this returns link: google.com
Your function now returns print
twice. First when you called the function, and second when you returned the function, and in this case actually returns None
because get_info
does not return anything.
This is evident here:
url = get_links('google.com')
>>this returns link: google.com
>>this returns link: google.com
url
>> *nothing happens*
The results of return are more apparent if it actually does something, for example:
def get_links(url):
url = "this returns link: {}".format(url)
return get_info(url)
def get_info(link):
return "get_info does something, {}".format(link)
url = get_links('google.com')
url
>>'get_info does something, this returns link: google.com'
If you do not use return, it just means the function will not return anything, which happens for example if you just want to print
the results as you did. You can further try this out by assigning a name like I did above to a function does has no return and the result will essentially be None
.
answered Nov 22 at 5:23
BernardL
2,3331829
2,3331829
I don't think he's calling the function twice. He's just asking what the difference is between calling it with or withoutreturn
.
– Barmar
Nov 22 at 5:37
I just meant to illustrate that the 2 calls inget_links
callget_info
twice, one withreturn
and one without does not return any value even when assigned to a variable because it is probably different than the way he intended it to work.
– BernardL
Nov 22 at 5:43
Read the comments in the code, it says "why this ... or this". He wants to know why he should use one or the other.
– Barmar
Nov 22 at 6:00
Perhaps. Why was too broad for me to answer and therefore I replied with an explanation of what it does.
– BernardL
Nov 22 at 6:10
If you haven't come across this post already, please take a look @BernardL.
– robots.txt
Nov 22 at 16:48
|
show 1 more comment
I don't think he's calling the function twice. He's just asking what the difference is between calling it with or withoutreturn
.
– Barmar
Nov 22 at 5:37
I just meant to illustrate that the 2 calls inget_links
callget_info
twice, one withreturn
and one without does not return any value even when assigned to a variable because it is probably different than the way he intended it to work.
– BernardL
Nov 22 at 5:43
Read the comments in the code, it says "why this ... or this". He wants to know why he should use one or the other.
– Barmar
Nov 22 at 6:00
Perhaps. Why was too broad for me to answer and therefore I replied with an explanation of what it does.
– BernardL
Nov 22 at 6:10
If you haven't come across this post already, please take a look @BernardL.
– robots.txt
Nov 22 at 16:48
I don't think he's calling the function twice. He's just asking what the difference is between calling it with or without
return
.– Barmar
Nov 22 at 5:37
I don't think he's calling the function twice. He's just asking what the difference is between calling it with or without
return
.– Barmar
Nov 22 at 5:37
I just meant to illustrate that the 2 calls in
get_links
call get_info
twice, one with return
and one without does not return any value even when assigned to a variable because it is probably different than the way he intended it to work.– BernardL
Nov 22 at 5:43
I just meant to illustrate that the 2 calls in
get_links
call get_info
twice, one with return
and one without does not return any value even when assigned to a variable because it is probably different than the way he intended it to work.– BernardL
Nov 22 at 5:43
Read the comments in the code, it says "why this ... or this". He wants to know why he should use one or the other.
– Barmar
Nov 22 at 6:00
Read the comments in the code, it says "why this ... or this". He wants to know why he should use one or the other.
– Barmar
Nov 22 at 6:00
Perhaps. Why was too broad for me to answer and therefore I replied with an explanation of what it does.
– BernardL
Nov 22 at 6:10
Perhaps. Why was too broad for me to answer and therefore I replied with an explanation of what it does.
– BernardL
Nov 22 at 6:10
If you haven't come across this post already, please take a look @BernardL.
– robots.txt
Nov 22 at 16:48
If you haven't come across this post already, please take a look @BernardL.
– robots.txt
Nov 22 at 16:48
|
show 1 more comment
up vote
1
down vote
return get_info(elem)
will call the get_info()
function, then take whatever it returned, and return that same value from get_links()
. It's roughly equivalent to:
temp = get_info(elem)
return temp
But since get_info()
doesn't return anything, it just prints the link, there's not much point in using it in a return
statement. Writing just
get_info(elem)
calls the function without doing anything with its return value (if it returned anything).
I can see a huge difference between them when I go through this script. The script in the answer has got thisreturn get_data(itemlink['href'])
without which I don't get any result. I meant I don't get any result when I doget_data(itemlink['href'])
.
– robots.txt
Nov 22 at 5:16
get_data
doesn't return anything, it just prints, like theget_info
function here.
– Barmar
Nov 22 at 5:37
add a comment |
up vote
1
down vote
return get_info(elem)
will call the get_info()
function, then take whatever it returned, and return that same value from get_links()
. It's roughly equivalent to:
temp = get_info(elem)
return temp
But since get_info()
doesn't return anything, it just prints the link, there's not much point in using it in a return
statement. Writing just
get_info(elem)
calls the function without doing anything with its return value (if it returned anything).
I can see a huge difference between them when I go through this script. The script in the answer has got thisreturn get_data(itemlink['href'])
without which I don't get any result. I meant I don't get any result when I doget_data(itemlink['href'])
.
– robots.txt
Nov 22 at 5:16
get_data
doesn't return anything, it just prints, like theget_info
function here.
– Barmar
Nov 22 at 5:37
add a comment |
up vote
1
down vote
up vote
1
down vote
return get_info(elem)
will call the get_info()
function, then take whatever it returned, and return that same value from get_links()
. It's roughly equivalent to:
temp = get_info(elem)
return temp
But since get_info()
doesn't return anything, it just prints the link, there's not much point in using it in a return
statement. Writing just
get_info(elem)
calls the function without doing anything with its return value (if it returned anything).
return get_info(elem)
will call the get_info()
function, then take whatever it returned, and return that same value from get_links()
. It's roughly equivalent to:
temp = get_info(elem)
return temp
But since get_info()
doesn't return anything, it just prints the link, there's not much point in using it in a return
statement. Writing just
get_info(elem)
calls the function without doing anything with its return value (if it returned anything).
answered Nov 22 at 5:09
Barmar
417k34240341
417k34240341
I can see a huge difference between them when I go through this script. The script in the answer has got thisreturn get_data(itemlink['href'])
without which I don't get any result. I meant I don't get any result when I doget_data(itemlink['href'])
.
– robots.txt
Nov 22 at 5:16
get_data
doesn't return anything, it just prints, like theget_info
function here.
– Barmar
Nov 22 at 5:37
add a comment |
I can see a huge difference between them when I go through this script. The script in the answer has got thisreturn get_data(itemlink['href'])
without which I don't get any result. I meant I don't get any result when I doget_data(itemlink['href'])
.
– robots.txt
Nov 22 at 5:16
get_data
doesn't return anything, it just prints, like theget_info
function here.
– Barmar
Nov 22 at 5:37
I can see a huge difference between them when I go through this script. The script in the answer has got this
return get_data(itemlink['href'])
without which I don't get any result. I meant I don't get any result when I do get_data(itemlink['href'])
.– robots.txt
Nov 22 at 5:16
I can see a huge difference between them when I go through this script. The script in the answer has got this
return get_data(itemlink['href'])
without which I don't get any result. I meant I don't get any result when I do get_data(itemlink['href'])
.– robots.txt
Nov 22 at 5:16
get_data
doesn't return anything, it just prints, like the get_info
function here.– Barmar
Nov 22 at 5:37
get_data
doesn't return anything, it just prints, like the get_info
function here.– Barmar
Nov 22 at 5:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424187%2ftrouble-understanding-the-difference-between-passing-result-to-another-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It sounds like you might be thinking of languages like Ruby which has implicit returns. Python doesn't have those and neither do most languages.
– pguardiario
Nov 22 at 6:23