The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Return statement
Discuss Return statement in the Python Programming forum on Dev Shed. Return statement Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 29th, 2013, 12:29 AM
|
|
Registered User
|
|
Join Date: Jan 2010
Posts: 9
Time spent in forums: 2 h 39 m 46 sec
Reputation Power: 0
|
|
|
Solved : Return statement
Hi
I got the following code which does what I want except one thing, if I do :
Code:
r = dectohex(39784)
print r
The result is None ??
It only works if I write print instead of return..
Code:
# IMPORTS
import string
# list of HEX to DEC correspondances
correspondances = {10:'A', 11:'B', 12:'C', 13:'D', 14:'E', 15:'F'}
# list hosting translated digits
hnumb = []
# convert function dec to hex
def dectohex(x):
mod = x%16
if mod > 9:
hdigit = correspondances.get(mod)
else:
hdigit = mod
hnumb.append(hdigit)
if x/16 == 0:
result = ''.join(str(i) for i in hnumb)
return result[::-1]
else:
dectohex(x/16)
Could someone explain to me why ?
Thank you
|

January 29th, 2013, 02:10 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | Originally Posted by chaying
Code:
if x/16 == 0:
result = ''.join(str(i) for i in hnumb)
return result[::-1]
else:
dectohex(x/16)
Could someone explain to me why ? |
Because your “else” branch lacks a “return”.
Your function is defective in many other ways as well. For example, since “hnumb” is not a local function, the result will be different if hnumb != [] to begin with. These kind of variables should all be local.
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)
|

January 29th, 2013, 02:29 AM
|
|
Registered User
|
|
Join Date: Jan 2010
Posts: 9
Time spent in forums: 2 h 39 m 46 sec
Reputation Power: 0
|
|
Hi Oscar
Thanks to stop by.
Sorry but I don't undertstand : 'lack of a return statement' ? What do you mean ? If I put a return statement :
Code:
else:
dectohex(x/16)
return
Wouldn't that be some 'dead code' : this code will never be reached, will it ?
edit: after test, it doesn't change anything.
And I modified my function inserting a :
after the function definition but I don't see how it could be a local variable as it would be reinitialized each time, wouldn't it ?
You said 'many ways', what other ways would you think about ? 
|

January 29th, 2013, 03:57 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | Originally Posted by chaying Sorry but I don't undertstand : 'lack of a return statement' ? What do you mean ? If I put a return statement :
Code:
else:
dectohex(x/16)
return
|
“return” without an argument is the same as “return None” which, incidentally, is also the default return value for a function without a return statement, so of course that doesn’t change anything.
But what do you expect the line previous to that does? Put a return before the “dectohex(x/16)” so the function might ultimately actually return something.
Quote: And I modified my function inserting a :
after the function definition but I don't see how it could be a local variable as it would be reinitialized each time, wouldn't it ? |
True, if you let it, but it’s still bad practice for functions to modify outside environment.
Edit: I might as well add this as an afterthought... One way to get rid of global variables is to use an optional function parameter:
Code:
def dectohex(x, hnumb=None):
# ...
if hnumb is None:
hnumb = []
hnumb.append(hdigit)
if x // 16 == 0:
return ''.join([str(i) for i in hnumb])[::-1]
else:
return dectohex(x // 16, hnumb)
Last edited by SuperOscar : January 29th, 2013 at 04:10 AM.
|

January 29th, 2013, 04:44 AM
|
|
Registered User
|
|
Join Date: Jan 2010
Posts: 9
Time spent in forums: 2 h 39 m 46 sec
Reputation Power: 0
|
|
Quote: | “return” without an argument is the same as “return None” which, incidentally, is also the default return value for a function without a return statement, so of course that doesn’t change anything. |
Ok, my bad..
Quote: | But what do you expect the line previous to that does? Put a return before the “dectohex(x/16)” so the function might ultimately actually return something. |
It's actually a real misunderstanding of the flow of execution here..
I really didn't see why with the initial statement, the function doesn't end systematically on the return from the if statement.
My understanding now is that without the return statement, it's like I call a new function wich does the same thing but that isn't related to any previous calculation : it looks like it's recursive but it's not. Am I right ?
Quote: One way to get rid of global variables is to use an optional function parameter:
Code:
def dectohex(x, hnumb=None):
# ...
if hnumb is None:
hnumb = []
hnumb.append(hdigit)
if x // 16 == 0:
return ''.join([str(i) for i in hnumb])[::-1]
else:
return dectohex(x // 16, hnumb)
|
Thanks a lot for the tip and your help ! 
|

January 29th, 2013, 06:05 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | Originally Posted by chaying My understanding now is that without the return statement, it's like I call a new function wich does the same thing but that isn't related to any previous calculation : it looks like it's recursive but it's not. Am I right ? |
It’s recursive all right. The main point is what happens after the recursive call is ended and the execution returns to the caller. In your code, the value obtained would simply be discarded. Adding the “return” makes the function ultimately have some end result.
|

January 29th, 2013, 06:50 AM
|
|
Registered User
|
|
Join Date: Jan 2010
Posts: 9
Time spent in forums: 2 h 39 m 46 sec
Reputation Power: 0
|
|
Ok I think I get it now..
I'm still going to run some flows to be sure but so, the ultimate 'return' that I thought would get me the result, only returns it to the last calling function, which holds the result until someone says to her to return it, or that the system discards it : what I didn't do.
I hope I get it right now.. If not, please keep in mind that I do "help 'my' landlady to take out her garbage"..
Thanks again anyway !
|

January 29th, 2013, 10:18 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | Originally Posted by chaying Ok I think I get it now..  |
No need to call the landlady, you got it right 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|