Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 29th, 2013, 12:29 AM
chaying chaying is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 9 chaying User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old January 29th, 2013, 02:10 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 6 m 48 sec
Reputation Power: 65
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)

Reply With Quote
  #3  
Old January 29th, 2013, 02:29 AM
chaying chaying is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 9 chaying User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 :
Code:
global hnumb

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 ?

Reply With Quote
  #4  
Old January 29th, 2013, 03:57 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 6 m 48 sec
Reputation Power: 65
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 :
Code:
global hnumb

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.

Reply With Quote
  #5  
Old January 29th, 2013, 04:44 AM
chaying chaying is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 9 chaying User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 !

Reply With Quote
  #6  
Old January 29th, 2013, 06:05 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 6 m 48 sec
Reputation Power: 65
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.

Reply With Quote
  #7  
Old January 29th, 2013, 06:50 AM
chaying chaying is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 9 chaying User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 !

Reply With Quote
  #8  
Old January 29th, 2013, 10:18 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 6 m 48 sec
Reputation Power: 65
Quote:
Originally Posted by chaying
Ok I think I get it now..


No need to call the landlady, you got it right

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Return statement

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap