You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: global_&_return.rst
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
-
Global & Return
1
+
Global and Return
2
2
---------------
3
3
4
-
You might have encountered some functions written in python which have a
4
+
You might have encountered some functions written in Python which have a
5
5
``return`` keyword in the end of the function. Do you know what it does? It
6
-
is similar to return in other languages. Lets examine this little
6
+
is similar to return in other languages. Let's examine this little
7
7
function:
8
8
9
9
.. code:: python
@@ -15,7 +15,7 @@ function:
15
15
print(result)
16
16
# Output: 8
17
17
18
-
The function above takes two values as input and then output their
18
+
The function above takes two values as input and then outputs their
19
19
addition. We could have also done:
20
20
21
21
.. code:: python
@@ -28,11 +28,11 @@ addition. We could have also done:
28
28
print(result)
29
29
# Output: 8
30
30
31
-
So first lets talk about the first bit of code which involves the
31
+
So first let's talk about the first bit of code which involves the
32
32
``return`` keyword. What that function is doing is that it is assigning
33
33
the value to the variable which is calling that function which in our
34
-
case is ``result``. In most cases and you won't need to use the
35
-
``global`` keyword. However lets examine the other bit of code as well
34
+
case is ``result``. In most cases you won't need to use the
35
+
``global`` keyword. However, let's examine the other bit of code as well
36
36
which includes the ``global`` keyword. So what that function is doing is
37
37
that it is making a global variable ``result``. What does global mean
38
38
here? Global variable means that we can access that variable outside the
@@ -125,11 +125,11 @@ Or by more common convention:
125
125
print(profile_age)
126
126
# Output: 30
127
127
128
-
Keep in mind that even in the above example we are returning a tuple (despite the lack of paranthesis) and not separate multiple values. If you want to take it one step further, you can also make use of `namedtuple <https://docs.python.org/3/library/collections.html#collections.namedtuple>`_. Here is an example:
128
+
Keep in mind that even in the above example we are returning a tuple (despite the lack of parenthesis) and not separate multiple values. If you want to take it one step further, you can also make use of `namedtuple <https://docs.python.org/3/library/collections.html#collections.namedtuple>`_. Here is an example:
129
129
130
130
.. code:: python
131
131
132
-
from collections import namedtuple
132
+
from collections import namedtuple
133
133
defprofile():
134
134
Person = namedtuple('Person', 'name age')
135
135
return Person(name="Danny", age=31)
@@ -157,4 +157,4 @@ Keep in mind that even in the above example we are returning a tuple (despite th
157
157
print(age)
158
158
#31
159
159
160
-
This is a better way to do it along with returning ``lists`` and ``dicts``. Don't use ``global`` keyword unless you know what you are doing. ``global`` might be a better option in a few cases but is not in most of them.
160
+
This is a better way to do it, along with returning ``list`` and ``dict``. Don't use ``global`` keyword unless you know what you are doing. ``global`` might be a better option in a few cases but is not in most of them.
0 commit comments