
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level concept in data structures, combined with dynamic typing and dynamic binding, presents it as a very attractive language for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components or services. Python supports modules and packages, thereby encouraging program modularity and code reuse. But sometimes Python's simple, easy-to-learn syntax can mislead Python developers and they create some general python mistakes – especially those who are freshers to the language. Python mistakes that freshers make result in missing some of its subtleties and underestimating the potential of the diverse Python language. Below the article presents the top 10 atrocious python mistakes that freshers often make.
Let's start with a beginner's mistake that can give you some issues if it isn't detected promptly. Since Python is a scripting language, you can define functions that you can call in REPL mode. This simple code will execute the super_fun function when it's called from the CLI python no_main_func.py. But what happens when you want to reuse the code as a module in a notebook? Here, super_fun is executed automatically when you import your script. If your function performs some expensive computational operations or invokes a process that spawns multiple threads. It is the most common mistake for freshers.
Python language permits you to specify that a function argument is optional by offering a default value for it. No doubt this is a wonderful feature of the Python language but sometimes it can lead to some confusion for freshers when the default value is mutable. This is among the top 10 atrocious python mistakes that freshers make unconsciously.
Python scope resolution is relying on what is known as the LEGB rule, which is considered a shorthand for Local, Enclosing, Global, and Built-in. Seems straightforward enough, right? Well, actually, there are some subtleties to the way this works in Python, which brings us to the common more advanced Python programming error and this is among the top python mistakes that freshers do.
Deleting an item from a list or array while iterating over it is a Python problem that is well-known to any experienced software developer. But sometimes becomes a problem for freshers. Even well-experienced Python developers can be unintentionally bitten by this in code that is much more complex. This is among the top 10 atrocious python mistakes that make developers uneasy. But fortunately, Python incorporates several elegant programming paradigms that, when applied properly, can offer simplified and streamlined code. Another benefit of this is that simpler code is less likely to be bitten by the accidental-deletion-of-a-list-item-while-iterating-over-it bugs.
This occurs due to Python's late binding behavior which says that the values of variables used in closures are looked up at the time the inner function is called.
One of the beauties of Python is the wealth of library modules that it comes with "out of the box". But if you are not consciously avoiding it, it is not that hard to run into a name clash between the name of one of your modules and a module with the same name in the standard library that ships with Python (for example, you might have a module named in your code, which would conflict with the standard library module of the same name). This can lead to the top python mistakes, such as importing another library which in turn tries to import the Python Standard Library version of a module but, since you have a module with the same name, the other package mistakenly imports your version instead of the one within the Python Standard Library. This is where bad Python errors happen.
The mistake that occurs here is in Python 3, the exception object is not accessible beyond the scope of the except block. The reason for this is that, otherwise, it would keep a reference cycle with the stack frame in memory until the garbage collector runs and purges the references from memory. More technical detail about this is available here.
Another top python mistakes from beginners are float point management in Python. Newbies often incorrectly think of float as a simple type. For example, the following code shows the difference between comparing the identifier of simple types (like int numbers) versus float types. Floating point types also have a subtle but important characteristic: their internal representation.
The definition of what should be considered a Boolean "true" value is a major source of confusion in several programming languages, and Python is no exception. This can be problematic because a variable can be undefined but later used in a comparison that will produce unexpected results.
Used as a constructor, the init function generates an object or allocates memory to a new class object which is used as a constructor when an object is created from a class and allows the class to initialize the class's attributes. However, common mistake Python developers make is to attempt to apply this method to return values. To fix this, just understand that the init method's function in Python is exclusively for use as a constructor.
Python late binds its variables in closures. This means that it calls upon the values of the variables that were returned the first time the affected function was called. This may not always be problematic for your code's purposes, but if it is, you can fix this by creating a closure that will bind immediately to its arguments, such as a default argument.
Join our WhatsApp Channel to get the latest news, exclusives and videos on WhatsApp
_____________
Disclaimer: Analytics Insight does not provide financial advice or guidance on cryptocurrencies and stocks. Also note that the cryptocurrencies mentioned/listed on the website could potentially be scams, i.e. designed to induce you to invest financial resources that may be lost forever and not be recoverable once investments are made. This article is provided for informational purposes and does not constitute investment advice. You are responsible for conducting your own research (DYOR) before making any investments. Read more about the financial risks involved here.