while you can access class attributes using an instance it's not safe to do so

Photo by Erik Mclean on Unsplash

In python, the instance of a class is referred to by the keyword self. Using this keyword you can access not only all instance attributes but also the class attributes. While it’s not an issue when trying to read the class attribute using self but trying to assign/modify the class attribute will not work.

In this article, I will demonstrate this using a simple example where we use a class attribute to assign and track the serial number of shipping containers.

Let’s begin with the example


Custom attributes are a must to relay domain-relevant information in your project

Code snippet
Code snippet
Photo by the author.

Working on a large project is challenging on many fronts. One of those challenges is to make sure the logs are relaying all relevant information for the project that could help with debugging issues and failures.

Chances are your project is already using Python’s industry-standard logging module for logging purposes, so we need to be able to extend the logging module to add this information.

Example

I work with pytest to develop my test automation solutions. When I am using logging for a project that consists of hundreds of tests (both unit tests and selenium tests), I need the name of…


when you are looking for a complete python solution to your REST API testing needs

This article is a continuation of the article I submitted about same time last year. Can’t believe its been an year now, so I wanted to go ahead and finish the series on this topic.

https://taverntesting.github.io/

I want to take a different approach this time around and try to solve a common problem that is encountered when setting up an API automation solutions on real projects. The solution to the problem as it turns out would be the exciting feature of tavern that you would also get to learn about. So let’s dive into it

When you are running your API…


pythons built-in exceptions are great but not good enough…

Photo by Romson Preechawit on Unsplash

New programmers are often confused about the topic of exception handling and look for best practices in adopting exception handling into their existing code but without proper knowledge of how exception handling works under the hood it can cause their programs to behave unexpectedly.

In this article I will present a simple use case where using the built-in python exception is insufficient and results in an obscure error message that I will work through and attempt to improve on starting with using the built-in exception initially and then gradually defining our own custom exception to demonstrate a somewhat realistic thinking…


prefer selling options to buying if you want to be a winner in the long run

Photo by Taras Chernus on Unsplash

Investing in the stock market is confusing for many beginners and even veterans believe it or not who don’t understand all the different strategies you can use to make money.

WallStreetBets on reddit is full of stories of investors losing all their life savings on crazy bets. You don’t have to be one of them. It’s pure gambling and has a very high probability of you losing money instead. To be fair there are clear winners too who have become millionaires overnight but surely they are a handful minority

Now there are a lot of different strategies you can use…


what happens when you blindly catch all exceptions in your program

Photo by Ryoji Iwata on Unsplash

New programmers are often confused about the topic of exception handling and look for best practices in adopting exception handling into their existing code but without proper knowledge of how exception handling works under the hood it can cause their programs to behave unexpectedly.

In this article I will not go into details about the workings of exceptions but instead I will try to show an undesirable consequence of not specifying an exception class in your exceptional handling logic. …


Why complicate such a trivial task using a blob of code when you can use glob?

Photo by Pakata Goh on Unsplash

Glob is an inbuilt Python module that is used to find path names in your filesystem which match a specific pattern. It takes its inspiration from the Unix shell-style of pattern matching.

So, What’s the Use Case?

Ok, we have an idea about the module now but what about the use case? Where might you be able to use this and potentially avoid using a blob of code and instead use glob’s built-in functions to achieve the same task in possibly a single line of code? Let’s take a look.

This is a very common task that can find its use in almost every Python…


Photo by Sean Lim on Unsplash

While Python is a great everyday language which finds its place in every single domain whether it be Finance, Data Science, Web Development, or IT Infrastructure; its usage in large scale projects is often limited and the well known reason given is in its dynamically typed nature which can make the code unreadable and its quite hard to tell what is going on at any given place

Since in large projects the code is more often read than written….

“Indeed, the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code…


when the __init__ method doesn’t support all the ways you want class users to be able to create instances of a class

Photo by Everton Vila on Unsplash

Class methods are great alternative constructors that can be used as factory methods to create instances of class by supporting different instantiation arguments.

Say you have a very simple Date class as shown below that can be instantiated by providing three arguments: a Year, a Month and a Day

A user would instantiate the class as following

date = Date(2020, 04, 05)
print(date)

which works just fine

> Date(2020, 04, 05)

But let’s say you want to provide users of your class alternative ways to create Date instances. For e.g. a user should be able to create a Date…


Why? Because it’s error-prone and confusing to the caller

Photo by Alexander Popov on Unsplash

Often, when reading Python code, you’ll come across utility functions that will return None. Although it might seem natural to return None in some cases, it should be avoided because it can be error-prone.

Let’s See Why Returning None Is Error-Prone

Let’s say you want to write a simple library function that divides two numbers.

The function returns None when the denominator is 0. This makes sense as the result is undefined so sending None sounds natural. However, the user of the function can incorrectly use it as shown below.

When the numerator is 0, the function would return 0, which is expected but look…

Imran Ali

QA Automation Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store