loading...
Cover image for 5 common mistakes made by beginner Python programmers

5 common mistakes made by beginner Python programmers

anuragrana profile image Anurag Rana Originally published at pythoncircle.com ・4 min read

Originally published at pythoncircle.com

During the initial days as python programmer, all of us face some or other type of weird bug in our code which, after spending multiple painful hours on StackOverflow, turns out to be not a bug but python feature. That's how things work in python. So below are the 5 most common mistakes most of the beginner python programmers make. Let's know a bit about them so that we can save a few hours of asking questions on Facebook pages and groups.

- Creating a copy of dictionary or lists.

Whenever you need to make a copy of a dictionary or list, do not simply use the assignment operator.

Wrong:

>>> dict_a = {"name": "John", "address":"221B Baker street"}
>>> dict_b = dict_a

Now if you edit/update the dict_b, dict_a will also be updated because by using assignment operator, you are trying to say that dict_b will point to the same object to which dict_a is pointing.

>>> dict_b["age"] = 26
>>> dict_b
{'address': '221B Baker street', 'name': 'John', 'age': 26}
>>> dict_a
{'address': '221B Baker street', 'name': 'John', 'age': 26}
>>> 

Correct:

Use the copy() or deepcopy() method.

>>> dict_c = dict_b.copy()
>>> dict_c["location"] = "somewhere"
>>> dict_c
{'address': '221B Baker street', 'name': 'John', 'age': 26, 'location': 'somewhere'}
>>> dict_b
{'address': '221B Baker street', 'name': 'John', 'age': 26}
>>> dict_a
{'address': '221B Baker street', 'name': 'John', 'age': 26}
>>> 

See the difference between copy and deepcopy.

- Dictionary keys.

Let's say we put the below values in a dictionary.

>>> dict_a = dict()
>>> dict_a
{}
>>> dict_a[1] = "apple"
>>> dict_a[True] = "mango"
>>> dict_a[2] = "melon"

If we try to print the dictionary, what will be the output. Let's see.

>>> dict_a
{1: 'mango', 2: 'melon'}

What just happened? where is the key True?
Remember Boolean class is the subclass of Integer. Integer equivalent of True is 1 and that of False is 0. Hence the value of key 1 is overwritten.

>>> isinstance(True, int)
True
>>> isinstance(False, int)
True
>>> True == 1
True
>>> False == 0
True
>>> 

- Updating lists or dictionaries.
Let's say you want to append an item to the list.

>>> list_a = [1,2,3,4,5]
>>> list_a = list_a.append(6)
>>> list_a
>>> # prints nothing

Try to update a dictionary.

>>> dict_a = {"a" : "b"}
>>> dict_a = dict_a.update({"c" : "d"})
>>> dict_a
>>> # prints nothing

Ok, let's try to sort a list.

>>> list_b = [2,5,3,1,7]
>>> list_b = list_b.sort()
>>> list_b
>>> # prints nothing

Why nothing is being printed? What are we doing wrong?

Most the sequence object methods like sort, update, append, add, etc works in place to increase performance by avoiding to create a separate copy un-necessarily.

Do not try to assign the output of such methods to the variable.

Right way:

>>> list_a = [1,2,3,4,5]
>>> list_a.append(6)
>>> dict_a = {"a" : "b"}
>>> dict_a.update({"c" : "d"})
>>> dict_a
{'c': 'd', 'a': 'b'}
>>> list_a.sort()
>>> list_a
[1, 2, 3, 4, 5, 6]

- Interned Strings.
In some cases, python tries to reuse the existing immutable objects. String interning is one such case.

>>> a = "gmail"
>>> b = "gmail"
>>> a is b
True

Here we tried to create two different string objects. But when checked if both the objects are same, it returned True. This is because python didn't create another object b but pointed the b to the first value "gmail".

All strings of length 1 are interned. A string having anything except ASCII characters, digits and underscore in them are not interned.

Let's see.

>>> a = "@gmail"
>>> b = "@gmail"
>>> a is b
False
>>> 

Also, remember == is different than is operator. == checks if values are equal or not while is checks if both the variable are pointing to the same object.

>>> a = "@gmail"
>>> b = "@gmail"
>>> a is b
False
>>> a == b
True
>>> 

So keep the above point in mind while using immutable strings or == or is operator.

- Default arguments are evaluated only once.

Consider the below example.

def func(a, lst=[]):
    lst.append(a)
    return lst

print(func(1))
print(func(2))

What do you think will be the output of the above two print statements?

Let's try to run it.

>>> def func(a, lst=[]):
...     lst.append(a)
...     return lst
... 
>>> print(func(1))
[1]
>>> print(func(2))
[1, 2]
>>> 

Why the output is [1,2] in the second case. Shouldn't it be just [2]?

So the catch here is, default arguments of a function are evaluated just once. On first call i.e func(1), list lst is evaluated and is found empty hence 1 is appended to it. But on the second call, the list is already having one element hence output is [1,2]

Bonus: Don't mix spaces and tabs. Just don't. You will cry.

More from pythoncircle.com:

Discussion

pic
Editor guide
Collapse
pinotattari profile image
Riccardo Bernardini

Default arguments are evaluated only once.

Ouch! This was quite surprising. It reminds me of old FORTRAN days when you could change the value of a constant. (FORTRAN passes everything by address, this means that if you want to pass a constant to a function, you need to write the constant somewhere and pass the address. If the function changes the parameter, the constant will change.)

Collapse
qfarhan profile image
Q Farhan

Default arguments are evaluated only once. -> Mind blown. Does it mean one needs to keep track of if a function is called in the script already?

But just found a relevant SO discussion if someone interested. :-)

Great post, btw.

Collapse
jbence2001 profile image
Bence Juhász

What's the thing about spaces and tabs? I mean okay I saw it already, that this is a harsh point for programmers, but why? (absolutely newbie here)

Collapse
anuragrana profile image
Anurag Rana Author

Bence,
Python doesn't support mixing tabs and spaces for indentation.

For other languages as well we should stick to one thing, either tab or spaces. This is because of different editors display tabs/spaces differently.

Collapse
cyberspy profile image
Adam

I'm pretty sure that python 2.7 allows mixed tabs and spaces (not that it is recommended) but python 3.x will error if you try that.
PEP 8 recommends spaces, but you can use tabs if that's what a project already uses.
python.org/dev/peps/pep-0008/#tabs...

Collapse
jbence2001 profile image
Bence Juhász

Thanks for the reply. So I can use tabs or spaces as well but not in the same code, right?

Thread Thread
anuragrana profile image
Collapse
vdedodev profile image
Vincent Dedo

I consider myself fairly advanced in python, but I hadn't heard the term "interned" for strings. Thanks for pointing that out.

Collapse
lmbarr profile image
Luis Miguel

same here.

Collapse
rolandcsibrei profile image
Roland Csibrei

Thanks for sharing your knowledge.

Collapse
ingles98 profile image
Filipe Reis

Wait how is the last example true?? Lst is under the function scope only...

Collapse
rhymes profile image
rhymes

lst is evaluated when the file is interpreted, hence it's always the same thing in memory. It's one of the gotchas of Python, you can see it here:

>>> def func(a, lst=[]):
...     print(id(lst))
...
>>> func(3)
4451985672
>>> func(4)
4451985672

As you can see lst points to the same object in memory.

If you pass an argument it will substitute the one defined at evaluation time:

>>> r = []
>>> def func(a, lst=[]):
...     lst.append(a)
...     return lst
...
>>> func(1, r)
[1]
>>> func(2, r)
[1, 2]
>>> r
[1, 2]
>>> func(4)
[4]

Notice how when I don't pass an external list as an argument, it starts using the default one.

Collapse
johncip profile image
jmc

The names are under function scope, but the values are stored as entries in a collection (tuple I think?) called func_defaults (__defaults__ in 3). It's attached to the function object, so I think that means it has the same scope as the declaration.

Python 🙄

Collapse
prahladyeri profile image
Prahlad Yeri

You are right in that it shouldn't happen as per the rules of the scope (variables defined within function scope should be destroyed once they leave that scope) but still, it happens! You can call it a bug of the cpython interpreter though the chances of falling in this trap is very less. You'll have to be careless enough to accept a default parameter and not handle its value at all in the function and just let it free (which is quite rare).