function/method assigment

V

viscroad

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

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

Thanks!
 
S

Steve Holden

I have a confusion when I do some practice, the code and output are as
following,

print 'In fun()....'


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
 
J

Jakub Stolarski

I have a confusion when I do some practice, the code and output are as
following,


print 'In fun()....'


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.
 
B

Bruno Desthuilliers

(e-mail address removed) a ¨¦crit :
I have a confusion when I do some practice, the code and output are as
following,

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').

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.

Which is the expected result

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.
This prints the representation of the object bound to name testfun2 -
here, function fun.
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
 
7

7stud

I have a confusion when I do some practice, the code and output are as
following,


print 'In fun()....'


<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.
 
7

7stud

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top