Hi,
The more I read here in discussion forum, the more I am getting confused.
Till now, my understanding was ,
every python function should have 'return ’ statement otherwise it will print None.
In other words, a function without an explicit ‘return’ statement/keyword returns ‘None’.
Here, in this below code:
List is sorted with the help of sort() and returned.
>>> k = [14, 631, 4, 51358, 50000000]
>>> k.sort()
>>> k
[4, 14, 631, 51358, 50000000]
In this below code:
After sorting the value is stored to a variable ‘l’ and printed.
It is not returned, so it is printing None.
>>> k = [14, 631, 4, 51358, 50000000]
>>> l = k.sort()
>>> l
>>> print(l)
None
Is my understanding right? Or am I missing something.
Looking forward to few inputs so that I cover in-depth meaning of Default return topic.
Many thanks in advance.
mtf
August 11, 2020, 3:34am
24
>>> a = 42
>>> a
42
>>>
Notice how None
doesn’t even take a line?
Hi,
Yes, Now I understood the true meaning of ‘None’
‘None’ == ‘No Value at all’.
‘None’ != zero / empty string/ False
None is a datatype of its own (NoneType) and only None can be None .
Thanks.
1 Like
mtf
August 11, 2020, 3:46am
26
Be sure to add the understanding that methods very often return, None
. They have an execution context upon which to operate. In situ, as it were.
x.sort() # Nonassignable.
takes place on x
.
sorted(x) # Assignable.
takes place on a copy of x
.
1 Like
hey @mtf
“What’s in common with these two functions that return None
? They both have side-effects besides returning a value.”
can you explain this a little?
(in Default Return)
mtf
October 24, 2020, 11:46pm
28
Any function that performs in place will have no return value expected save for the default return, None
.
Please post a link to the lesson where you found that narrative.
mtf
October 25, 2020, 1:46am
30
That lesson compares the print()
function to the list.sort()
method, which is also a function. Both of them return None
every time. Their respective side effects are sending the argument to the standard output (console) and sorting the array in object context.
I am sorry I am having trouble with the last sentence
mtf
October 25, 2020, 10:09am
32
>>> print ("this expression")
this expresssion # side effect
>>>
>>> a = [6,2,8,3,1,9,4]
>>> a.sort()
>>> a
[ 1, 2, 3, 4, 6, 8, 9 ] # side effect
>>>