diff --git a/global_&_return.rst b/global_&_return.rst index 39a75d4..0661752 100644 --- a/global_&_return.rst +++ b/global_&_return.rst @@ -1,9 +1,9 @@ -Global & Return +Global and Return --------------- -You might have encountered some functions written in python which have a +You might have encountered some functions written in Python which have a ``return`` keyword in the end of the function. Do you know what it does? It -is similar to return in other languages. Lets examine this little +is similar to return in other languages. Let's examine this little function: .. code:: python @@ -15,7 +15,7 @@ function: print(result) # Output: 8 -The function above takes two values as input and then output their +The function above takes two values as input and then outputs their addition. We could have also done: .. code:: python @@ -28,11 +28,11 @@ addition. We could have also done: print(result) # Output: 8 -So first lets talk about the first bit of code which involves the +So first let's talk about the first bit of code which involves the ``return`` keyword. What that function is doing is that it is assigning the value to the variable which is calling that function which in our -case is ``result``. In most cases and you won't need to use the -``global`` keyword. However lets examine the other bit of code as well +case is ``result``. In most cases you won't need to use the +``global`` keyword. However, let's examine the other bit of code as well which includes the ``global`` keyword. So what that function is doing is that it is making a global variable ``result``. What does global mean here? Global variable means that we can access that variable outside the @@ -125,11 +125,11 @@ Or by more common convention: print(profile_age) # Output: 30 -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 `_. Here is an example: +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 `_. Here is an example: .. code:: python - from collections import namedtuple + from collections import namedtuple def profile(): Person = namedtuple('Person', 'name age') 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 print(age) #31 -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. +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.