473,654 Members | 3,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a question about "return"

In one of the examples in the book I'm reading, it says:

def __init__(self):
...
...
...
return

It has nothing after "return". I expected it to have some number like 0
or 1.

What does it mean to have nothing after return?

Why do we even include "return" if we are not putting any value after?

Thanks :)

Regards,

Brian

Jul 4 '06 #1
4 1312
On 2006-07-04, yaru22 <ya****@gmail.c omwrote:
What does it mean to have nothing after return?
It means to return None.
Why do we even include "return" if we are not putting any
value after?
If it's the last statement in the function, you don't need it.
Some people do it just as a formal indicate that that's the end
of the function.

OTOH, sometimes people want to return explicitly from somewhere
within the function:

def foo(x):
if x<0:
return
else:
whatever

--
Grant Edwards grante Yow! Two with FLUFFO,
at hold th' BEETS...side of
visi.com SOYETTES!
Jul 4 '06 #2
"yaru22" <ya****@gmail.c omwrote:
>In one of the examples in the book I'm reading, it says:

def __init__(self):
...
...
...
return

It has nothing after "return". I expected it to have some number like 0
or 1.

What does it mean to have nothing after return?
It means "the function is over, go back to the caller now".
>Why do we even include "return" if we are not putting any value after?
If it is the last statement in the function, then you are right, it serves
no purpose. Some people include one for completeness -- a coding standard
that "all paths must have a return statement", for instance.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 4 '06 #3
"yaru22" wrote:
In one of the examples in the book I'm reading, it says:

def __init__(self):
...
...
...
return

It has nothing after "return". I expected it to have some number like 0 or 1.

What does it mean to have nothing after return?
"return" is the same thing as "return None"

http://pyref.infogami.com/return
Why do we even include "return" if we are not putting any value after?
a plain "return" at the end of a function is entirely optional:
>>def func1():
.... print
....
>>def func2():
.... print
.... return
....
>>def func3():
.... print
.... return None
....
>>import dis
dis.dis(func1 )
2 0 PRINT_NEWLINE
1 LOAD_CONST 0 (None)
4 RETURN_VALUE
>>dis.dis(func2 )
2 0 PRINT_NEWLINE
3 1 LOAD_CONST 0 (None)
4 RETURN_VALUE
>>dis.dis(func3 )
2 0 PRINT_NEWLINE
3 1 LOAD_CONST 0 (None)
4 RETURN_VALUE

</F>

Jul 4 '06 #4

"yaru22" wrote in message
news:11******** *************@m 79g2000cwm.goog legroups.com...
In one of the examples in the book I'm reading, it says:

def __init__(self):
...
return

It has nothing after "return". I expected it to have some number like 0
or 1. What does it mean to have nothing after return?
Yes, it has some special value. The value is said to be None.
Better to say -- the doc says:

"None
This type has a single value. There is a single object with this
value. This object is accessed through the built-in name None.
It is used to signify the absence of a value in many situations,
e.g., it is returned from functions that don't explicitly return
anything. Its truth value is false.
Why do we even include "return" if we are not putting any value after?
If you do not use return in your function, it is the same as if
you used return without an argument at the end of the body.
It will return None.

If you use return, the function returns immediately to the caller.
It need not to be the last command. You may want to return
earlier.

pepr
Jul 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
2218
by: Christopher J. Bottaro | last post by:
This post is just the culmination of my thoughts and discussions with my coworkers on Python. If you are not interested, please skip over it. At my work, we are developing a product from scratch. It is completely modular and the modules communicate via SOAP. Because of that, we can implement individual modules in any language of our choosing (so long as they have good SOAP libs). I chose to do all mine in Python because I'm a huge...
32
8835
by: Mike Machuidel | last post by:
Hi, I'm a game developer programming mostly in C and ASM for about 7 years. Today at work a colleague (a C++ programmer) yelled at me I'm a bad C programmer because I use "return(0);" instead of "return 0;". He explained that "return" is not a function but a stament, like I didn't know already. The other colleagues also argreed with him :(. Can someone please explain what's so wrong about using "return" with
10
2599
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some of that software. That subject seems not to be addressed, at least not directly, in the C FAQ where FAQ 5.2 seems most relevant. References: * C FAQ 5.2 Null pointers (Including conditions where "casting" of null pointer...
15
6723
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int i = 0,j; j = sample(0);
0
2552
by: Joeyej | last post by:
Hi - I'm trying to move/use a web form (containing some javascript field checks) previously hosted on a Windows 2000 server. However, the FORM METHOD="post..." command in the form (shown below) now renders "Page not Found" when launched on the new Windows 2003 server (ws-server1). The form is designed to use the write.asp program to write to an Acccess database and is successful on any Windows 2000 server. This is frustrating because...
1
2012
by: vl106 | last post by:
char* foo () { return "abc"; } I compiled the above code both with MSVC and GCC for PPC. The string "abc" is generated as a global entity. Thus (1) foo doesn't return a temporary and (2) no deallocation is necessary. What does the standard say about this? Is this a "feature" I can rely on on
13
3996
by: kurtj | last post by:
Hello Gurus: I have a validation script (below) that is somehow messed up. If the Name field is blank, I get the alert message, then the browser window goes to a blank document with the word "false" on it. What the ?!?!?! To test, I commented out the 'return false;' code in the second IF block, so now if there is a value in Name then I get the alert message for Email and the page stays put.
32
2195
by: Axel Bock | last post by:
Hi all, I am trying to get my head around what happens if I return a class object from a function. It seems C++ (MinGW) does not invoke the copy constructor if I do something like this: SomeObj foo() { SomeObj X;
5
3888
by: Ben | last post by:
Hi; I use ain asp.net the CreateUserWizard control for new users. When it's done, i want to redirect them to another page (modifyprofile.aspx). This works with the code below. My question is: if i suppress the line "return false" in the javascript function profile(), the user is created but not redirected to the other aspx page. Can anybody explain me why?
0
8285
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8814
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8591
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2709
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.