Posts Tagged ‘nuances’

How to Avoid the 7 Most Frequent Mistakes in Python Programming

Monday, September 9th, 2019

python-programming-language-logo

Python is very appealing for Rapid Application Development for many reasons, including high-level built in data structures, dynamic typing and binding, or to use as glue to connect different components. It’s simple and easy to learn but new Python developers can fall in the trap of missing certain subtleties.

Here are 7 common mistakes that are harder to catch but that even more experienced Python developers have fallen for.

 

1. The misuse of expressions as function argument defaults

Python allows developers to indicate optional function arguments by giving them default values. In most cases, this is a great feature of Python, but it can create some confusion when the default value is mutable. In fact, the common mistake is thinking that the optional argument is set to whatever default value you’ve set every time the function argument is presented without a value. It can seem a bit complicated, but the answer is that the default value for this function argument is only evaluated at the time you’ve defined the function, one time only.  

how-to-avoid-the-7-most-frequent-mistakes-in-python-programming-3

 

2. Incorrect use of class variables

Python handles class variables internally as dictionaries and they will follow the Method Resolution Order (MRO). If an attribute is not found in one class it will be looked up in base classes so references to one part of the code are actually references to another part, and that can be quite difficult to handle well in Python. For class attributes, I recommend reading up on this aspect of Python independently to be able to handle them.

how-to-avoid-the-7-most-frequent-mistakes-in-python-programming-2

 

3. Incorrect specifications of parameters for exception blocks

There is a common problem in Python when except statements are provided but they don’t take a list of the exceptions specified. The syntax except Exception is used to bind these exception blocks to optional parameters so that there can be further inspections. What happens, however, is that certain exceptions are then not being caught by the except statement, but the exception becomes bound to parameters. The way to get block exceptions in one except statement has to be done by specifying the first parameter as a tuple to contain all the exceptions that you want to catch.

how-to-avoid-the-7-most-frequent-mistakes-in-python-programming-1
 

4. Failure to understand the scope rules

The scope resolution on Python is built on the LEGB rule as it’s commonly known, which means Local, Enclosing, Global, Built-in. Although at first glance this seems simple, there are some subtleties about the way it actually works in Python, which creates a more complex Python problem. If you make an assignment to a variable in a scope, Python will assume that variable is local to the scope and will shadow a variable that’s similarly named in other scopes. This is a particular problem especially when using lists.

 

5. Modifying lists during iterations over it

 

When a developer deletes an item from a list or array while iterating, they stumble upon a well known Python problem that’s easy to fall into. To address this, Python has incorporated many programming paradigms which can really simplify and streamline code when they’re used properly. Simple code is less likely to fall into the trap of deleting a list item while iterating over it. You can also use list comprehensions to avoid this problem.

how-to-avoid-the-7-most-frequent-mistakes-in-python-programming-8

       

6. Name clash with Python standard library

 

Python has so many library modules which is a bonus of the language, but the problem is that you can inadvertently have a name clash between your module and a module in the standard library. The problem here is that you can accidentally import another library which will import the wrong version. To avoid this, it’s important to be aware of the names in the standard library modules and stay away from using them.

how-to-avoid-the-7-most-frequent-mistakes-in-python-programming-5

 

7. Problems with binding variables in closures


Python has a late binding behavior which looks up the values of variables in closure only when the inner function is called. To address this, you may have to take advantage of default arguments to create anonymous functions that will give you the desired behavior – it’s either elegant or a hack depending on how you look at it, but it’s important to know.

 

 

how-to-avoid-the-7-most-frequent-mistakes-in-python-programming-6

Python is very powerful and flexible and it’s a great language for developers, but it’s important to be familiar with the nuances of it to optimize it and avoid these errors.

Ellie Coverdale, a technical writer at Essay roo and UK Writings, is involved in tech research and projects to find new advances and share her insights. She shares what she has learned with her readers on the Boom Essays blog.

Sjecas li se dolly bell? – Do you remember Dolly Bell? – A classic serbian movie by Emil Kosturica

Tuesday, September 27th, 2011

Sjecas li se Dolly Bell / Do you remember dolly bell?

Sjecas li se dolly is a piece of classic in the well known Kosturica genre, the movie is from the distant 1981. The movie action takes place in communistic Yugoslavia. It clearly contains anti-communistic nuances. I’m really amazed that this movie see the light of the day in the early ’90s while still communism had strong influence on information media in Yugoslavia.

As I’ve lived until the age of 7 in communism and post-communism (and experienced myself communism), the movie was especially interesting to see. In the family in the movie I can see many things I’ve seen and suffered many of the anti-human communistic bull-shit in my own family in my boy years.
Communism has cripppled us the Bulgarians as a nation and destroyed any society which it was in (clearly observable in all post-communistic countries).

Interesting thing to notice among the communistic Marxist ideas in the plot is the growing influence of the Western World (seen in the anti social behavior of the actors),the enthusiasm to look for occult of the main actor the teenager boy – (Dino), the desire to look follow Italian western culture etc.
The movie also keeps the mark of the negativism and crazyness which is so distinct about all Kosturica movies I’ve seen. Anyways from an art point of view the movie is a real master piece.

The movie plot takes place in the so conflict area of Sarajevo, a place predominated by Muslims. What is shocking about the movie considering its time of make, is the explicit erotic and sexually related scenes. The censorship in communistic times was quite severe so it’s amazing, how this anti-communistic movie containing society unacceptable scenes ever came to existence.
Do you remember Dolly Bell? is a drama movie, presenting a sad reality, we still partially continue to live in the Balkans. Though 20 years has passed since the fall of communism pitily not much has changed here…

Near the movie end there are some religious scenes as well obviously attempting to fill in the material emptiness of communism with something spiritual. The religious scene,is a muslim local tradition of a funeral preparations.
The relation between the movie and Islam is understandable as Kosturica had some Bosnian Muslim roots from the line of his father. This kind of muslim influence is also observable on the other Kosturica movies as well.
Nowdays since 2005, Kosturica is officially Orthodox Christian baptized in Savina Monastery which makes me happy as myself am Orthodox Christian 😉