3 simple Python concepts that will advance your career

Orestis Zekai
Geek Culture
Published in
3 min readFeb 14, 2022

Python is a programming language created in 1991 by Guido Van Rossum. Even though it has existed for a long time, only lately has caught the attention of the industry. In the last years, more and more companies are starting projects in Python, mostly for the fast development it offers. As a consequence, demand for Python developers is increasing and will keep increasing the next years. The following concepts are essential for any developer that wants to ride that wave in the future.

  1. Context managers
    Context managers allow you to allocate and release (manage) resources optimally. There resources, if not handled properly, can produce some really weird banks that will have you scratching your head. Context managers make sure that all aspects of a resource are handled properly. You can use them, by utilizing the with keyword. The most usual use case for using context managers is for manipulating files. After performing an operation on a file, it needs to be properly closed. Context managers can do this for you easily, by skipping the nitty gritty details:
with open('myfile.xtx', 'r') as f:
content = f.read()

Notice that we never called the f.close() method. The context manager handled it automatically for us, and it would try to do is as well, even if an exception was raised. There are many use cases that context managers can be used (i.e. aiohttp.ClientSession ) and of course, you can create your own.

2. Type hinting
Type hinting enables you to write clean, self explanatory code. The way you apply it is by “hinting” the type of a parameter and the return value of a function. For example, we want to validate the text input of a user is always an integer. To achieve that, we write a function that returns True of False based on our validations:

def validate_integer(user_input):
...

Now that you know what this function does, it is pretty easy to understand by looking at the definition. But, it would not be that easy if you were not given the description above. What is the type of the user_input parameter? Where does it come from? Is it already an integer? What if it is not? Does the function return anything, or just raises an exception? These questions can be answered, by refactoring the code to this:

def validate_integer(user_input: str) -> bool:
...

Now this function is easier to be interpreted, even by someone who reads this for the first time.

3. Understanding lists and dictionaries
This is a common misconeption for new developers. Let’s say you create a list a and then, assign this list to a new variable:

>>> a = [1, 2, 3]
>>> b = a

Now, try appending a new value in the b list and then print both lists:

>>> b.append(4)
>>> print(b)
[1, 2, 3, 4]
>>> print(a)
[1, 2, 3, 4]

What is weird here, is that the new value has been appended to both lists! This happens because when assigning lists in Python, unless otherwise specified, the list is not copied. Instead, a new reference to this list is created.

Variable b is just a reference to the list

This means that operations in both variables will be reflected to the same list. In order to make a copy of the list, you need to use the .copy() method:

>>> a = [1, 2, 3]
>>> b = a.copy()
>>> b.append(4)
>>> print(b)
[1, 2, 3, 4]
>>> print(a)
[1, 2, 3]

The points described above, are only some of the Python insights experienced developers are keeping in mind. Of course, as in any language, practice makes perfect.

--

--