Few Things To Avoid In Python

Python is easy to use, easy to learn , and a general-purpose high-level programming language that is one of the top choices for a beginner these days. If you are a python developer, below are some common python programming mistakes that you should probably avoid to make you Mumma proud.


1) Use Class Variables Carefully

Before we discuss, check out the below example,

class A():
num = 1
class B(A):
class C(A):
print(A.num, B.num, C.num)
>>> 1 1 1

So above is a simple example of multiple inheritances in Python. And the code looks fine. Now let’s try and modify this variable using a child class

B.num = 2
print(A.num, B.num, C.num)
>>>1 2 1

This also looks completely fine and we can appreciate that we can modify the value of the variable “num”
Now, check this,

A.num = 3
print(A.num, B.num, C.num)
>>> 3 2 3

Amm, strange! How did the value of C.num change? So in the above code, since the attribute “num” is not found in class C, it will be looked up in its base class A. In other words, C doesn’t have its own “num” property, independent of A. Thus, references to C.num are in fact references to A.num. This causes a Python problem unless it’s handled properly.

So beware when you work with class variables in Python😅

2) Case sensitiveness

Python is Case sensitive! This means variables “num” and “Num” are different. And this is one of the most common mistakes that beginners in python come across and as a result, it flushes out errors…
Check this out,

num= 1
Traceback (most recent call last):
File "< stdin>", line 1, in < module>
NameError: name 'Num’ is not defined

3) Incorrect Indentation

In python it's all about indentation. To create a block of code, Python uses indentation online Java, C++, etc that uses curly braces. Many characteristics depend on indentation. Some indentation errors in Python are harder to spot than others.Thus one should always follow a consistent indentation pattern because many Python features rely on indentation. Thus, Indentation plays an important role in Python.

4) Variable Binding

This is a concept that beginners fail to understand and appreciate. Python has late-binding behavior. There is often confusion among Python developers regarding how Python binds its variables. What it means is that it binds its variables enclosures or in the surrounding global scope and hence the values of variables used in closures are looked up at the time the inner function is called.

5) Misusing __init__

In Python, constructors are represented by something called the __init__ method. Whenever an object is created, the __init__ method is called and thus it allocated memory and initialized the attributes of the class. Thus, trying to explicitly return a value from the init method can throw a bunch of errors and create problems in the code.

Check this,

class Student:
def __init__(self, name, marks): = name
self.marks = marks
self.avgmarks= marks*1.5*100
return self.avgsal
std = Student("John", 67)

In this example, the code is trying to return the average marks of an employee from the __init__ method.And thus, It’ll result in an error “TypeError: __init__() should return None”. So, to resolve this problem, one can either create a new method that will return this or create a getter property.

6) Copy Carefully

Check this example,

a = [2, 4, 8]
b = a
a[1] = 10
[2, 10, 8]

Problem solved💯

7) Function calls with default arguments

Functions with default arguments are an amazing feature in Python, For beginners. arguments are tricky to use. Since Python evaluates the expression in default arguments every time function is defined, you need to dynamically generate default arguments. Thus, one should take care of this…

8) Not using Comments and Doc Strings

Comments are an integral part of programming. Comments make code more readable and self-explained. What if you don’t use comments and docstrings? It can be a nightmare! In no time, code can go from 10 lines to 10,000 lines, and then debugging can be a pain. Thus, using comments and docstrings can be a lifesaver

Making mistakes is a part of life and programming. And mistakes can happen, but knowing what mistakes can occur and how to solve them is really important.

Want to learn Python? Try Programming Hero, a new way to learn to code

Related Post

related post

If you spend 3–4 hours daily for 7 days, you will become a Junior Python Developer. I am providing this guideline using this android app called: Programming Hero.

related post

I am taking all the examples from the Python learning app called Programming Hero: Coding Just Got Fun