473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

function/method assigment

I have a confusion when I do some practice, the code and output are as
following,
>>def fun():
print 'In fun()....'

>>testfun = fun()
In fun()....
>>print testfun
None
>>testfun2 = fun
print testfun2
<function fun at 0x00CC1270>
>>print testfun2()
In fun()....
None
>>>
what is 'testfun'? Why it is 'None'? And print testfun2(), what is the
meaning of 'None'?

Thanks!

Apr 13 '07 #1
5 1139
vi******@gmail.com wrote:
I have a confusion when I do some practice, the code and output are as
following,
>>>def fun():
print 'In fun()....'

>>>testfun = fun()
In fun()....
>>>print testfun
None
>>>testfun2 = fun
print testfun2
<function fun at 0x00CC1270>
>>>print testfun2()
In fun()....
None

what is 'testfun'? Why it is 'None'? And print testfun2(), what is the
meaning of 'None'?

Thanks!

When a function does not specifically return a value then its return
value is a particular value known as None, the only instance of the None
type.

So testfun is the result of calling the fun function, and it's None
because fun() does not return a value.

Since testfun2 is just another reference to the fun function, testfun2()
is None for exactly the same reasons.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 13 '07 #2
On Apr 13, 6:14 pm, viscr...@gmail.com wrote:
I have a confusion when I do some practice, the code and output are as
following,
>def fun():

print 'In fun()....'
>testfun = fun()
In fun()....
>print testfun
None
>testfun2 = fun
print testfun2

<function fun at 0x00CC1270>
>print testfun2()

In fun()....
None

what is 'testfun'? Why it is 'None'? And print testfun2(), what is the
meaning of 'None'?

Thanks!
Your 'fun' is the sam as:

def fun():
print 'In fun()....'
return None

So
testfun = fun()

First prints message and then assign None to testfun.

Apr 13 '07 #3
vi******@gmail.com a ¨¦crit :
I have a confusion when I do some practice, the code and output are as
following,
>>>def fun():
print 'In fun()....'
Function fun doesn't explicitelly return something, so it's return value
defaults to None (nb: None is a builtin object representing 'nothing').
>
>>>testfun = fun()
This calls fun, and binds the returned value (in this case, None) to
name testfun
In fun()....
and this is a side-effect of the execution of function fun.
>>>print testfun
None
Which is the expected result
>>>testfun2 = fun
This binds the function object fun to the name testfun2 (IOW, testfun2
is now an alias for fun). Remember that in Python, the parens are not
optional if you want to call a function - they are in fact the 'call
operator'. If you forget them, you get a reference to the function
object, not the result of calling the function.
>>>print testfun2
This prints the representation of the object bound to name testfun2 -
here, function fun.
<function fun at 0x00CC1270>
>>>print testfun2()
In fun()....
None
This first calls testfun2 (which is now another name for function fun),
triggering the printing of the string "in fun()..." as a side effects,
then print the return value of testfun2 (aka fun), which is still None.

HTH
Apr 13 '07 #4
On Apr 13, 10:14 am, viscr...@gmail.com wrote:
I have a confusion when I do some practice, the code and output are as
following,
>def fun():

print 'In fun()....'
>testfun = fun()
In fun()....
>print testfun
None
>testfun2 = fun
print testfun2

<function fun at 0x00CC1270>>>print testfun2()

In fun()....
None

what is 'testfun'? Why it is 'None'? And print testfun2(), what is the
meaning of 'None'?

Thanks!
Start with these rules:

1) all function calls in your code are replaced by the function's
return value.

2) if a function doesn't have a return statement, it automatically
returns None.

3 The '()' symbols make the function execute. Without those the
function won't execute.
Apr 13 '07 #5
On Apr 13, 10:14 am, viscr...@gmail.com wrote:
what is the
meaning of 'None'?
It's a value just like any other python value: 2, 7.5, "red", and it
evaluates to false in a conditional:

my_var = None

if not my_var:
print "bad data"

Apr 13 '07 #6

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

Similar topics

2
by: yopwojtek | last post by:
Hello All, i know from some tutorials, that copy constructor and assigment operator is not inherited from base to derived class. i think it make sense but i wrote a little example: class Base {...
3
by: Thomas Scheiderich | last post by:
I am curious as to why ASP.NET returns values a different way from VB or VB.net (or can you use both). In my one book I have it returning using a return statement ...
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
1
by: finerrecliner | last post by:
what i'm trying to accomplish: user clicks button. then can click 2 more times anywhere on the page and display his mouse coordinates. after those 2 clicks, go back to normal mouse behavior the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.