Skip to content

Commit 75702fe

Browse files
committed
added multiple return values section
1 parent 9134035 commit 75702fe

1 file changed

Lines changed: 61 additions & 6 deletions

File tree

global_&_return.rst

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ scope of the function as well. Let me demonstrate it with an example :
4848
print(result)
4949
5050
# Oh crap we encountered an exception. Why is it so ?
51-
# the python interpreter is telling us that we do not
52-
# have any variable with the name of result. It is so
53-
# because the result variable is only accessible inside
51+
# the python interpreter is telling us that we do not
52+
# have any variable with the name of result. It is so
53+
# because the result variable is only accessible inside
5454
# the function in which it is created if it is not global.
5555
Traceback (most recent call last):
56-
File "", line 1, in
56+
File "", line 1, in
5757
result
5858
NameError: name 'result' is not defined
5959
60-
# Now lets run the same code but after making the result
60+
# Now lets run the same code but after making the result
6161
# variable global
6262
def add(value1, value2):
6363
global result
@@ -71,4 +71,59 @@ So hopefully there are no errors in the second run as expected. In
7171
practical programming you should try to stay away from ``global``
7272
keyword as it only makes life difficult by introducing unwanted variables
7373
to the global scope.
74-
74+
75+
Multiple return values
76+
^^^^^^^^^^^^^^^^^^^^^^^
77+
78+
So what if you want to return two variables from a function instead of one? There are a couple of approaches which new programmers take. The most famous approach is to use `global` keyword. Let's take a look at a useless example:
79+
80+
.. code:: python
81+
82+
def profile():
83+
global name
84+
global age
85+
name = "Danny"
86+
age = 30
87+
88+
profile()
89+
print(name)
90+
# Output: Danny
91+
92+
print(age)
93+
# Output: 30
94+
95+
**Note:**Don't try to use the above mentioned method. I repeat, don't try to use the above mentioned method!
96+
97+
Some *return* a `tuple`, `list` or `dict` with the reqired values after the function terminates. It is one way and works like a charm.
98+
99+
.. code:: python
100+
101+
def profile():
102+
name = "Danny"
103+
age = 30
104+
return (name, age)
105+
106+
profile_data = profile()
107+
print(profile_data[0])
108+
# Output: Danny
109+
110+
print(profile_data[1])
111+
# Output: 30
112+
113+
What many programmers don't know is that you can return two separate values as well. Let's take a look at an example so that you can better understand it:
114+
115+
.. code:: python
116+
117+
def profile():
118+
name = "Danny"
119+
age = 30
120+
return name, age
121+
122+
name, age = profile()
123+
print(name)
124+
# Output: Danny
125+
126+
print(age)
127+
# Output: 30
128+
129+
This is a better way to do it along with returning `tuples`, `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.

0 commit comments

Comments
 (0)