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
+61-6Lines changed: 61 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,16 +48,16 @@ scope of the function as well. Let me demonstrate it with an example :
48
48
print(result)
49
49
50
50
# 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
54
54
# the function in which it is created if it is not global.
55
55
Traceback (most recent call last):
56
-
File "", line 1, in
56
+
File "", line 1, in
57
57
result
58
58
NameError: name 'result'isnot defined
59
59
60
-
# Now lets run the same code but after making the result
60
+
# Now lets run the same code but after making the result
61
61
# variable global
62
62
defadd(value1, value2):
63
63
global result
@@ -71,4 +71,59 @@ So hopefully there are no errors in the second run as expected. In
71
71
practical programming you should try to stay away from ``global``
72
72
keyword as it only makes life difficult by introducing unwanted variables
73
73
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
+
defprofile():
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
+
defprofile():
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
+
defprofile():
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