Deepening Your Knowledge by Reimplementing Existing Functions and Methods

Just as developing real proficiency in a natural language requires regular active practice, the same is true when learning a programming language. It is not enough to read about the language’s rules and study existing code; you also need to write as much code as possible yourself.

Obviously, if programming is someone’s profession, this kind of practice happens naturally. But for those learning a programming language whose daily activities do not require them to develop new programs, it is a good idea to come up with exercises of their own. These exercises can first be worked out conceptually and then implemented in the programming language being learned.

It is always a good idea to start with simple tasks so that the satisfaction of successfully solving them encourages you to take on increasingly difficult challenges. If you cannot think of a suitable exercise, another useful approach is to try creating your own versions of functions, methods, or classes that Python already provides. The goal is for our implementation to provide the same functionality as the existing one.

One advantage of this approach is that it is relatively easy to check whether our solution is correct. At the same time, we will discover that even solving a seemingly simple task like this requires quite a bit of theoretical and practical knowledge of the language. This turns what we have learned into active, usable knowledge. The mistakes we are almost certain to make along the way, and the process of correcting them, reinforce this knowledge even further.

With this in mind, let us practice by reimplementing, for example, the find() and count() string methods. In other words, we will write our own functions that provide the same functionality as these methods.

The exact behavior of these methods, the arguments they accept, and their return values can be found in the official Python documentation, or with a somewhat more detailed explanation and examples, in the e-book Python Knowledge Building Step by Step: From the Basics to The First Desktop Application. In brief, however, their purpose and usage are as follows:

If a variable named text refers to a string, the text.find(sub, start, end) method returns the index of the first occurrence of the substring specified by the sub argument. The search starts at the beginning of text. If sub is not found in the string, the method returns -1.

The text.count(sub, start, end) method accepts arguments similar to those accepted by find(). It returns the number of occurrences of the character sequence specified by sub.

For both methods, the optional start and end arguments can be used to limit the search to the range between the specified indices. If they are not provided, the entire string is searched.

Since negative indices can also be used for the start and end arguments, it is worth recalling how negative indexing works with sequences before implementing our own functions. The image below illustrates this:

You can see below the definitions of the find() and count() functions we implemented. The comments help explain how they work.

The function bodies are not particularly complicated, yet even in a case like this, we need to be familiar with quite a few language features and constructs in order to design and write the appropriate code. We use a conditional expression, for and while loops, an assignment expression, conditional branching, slicing, augmented assignment, and exception handling.

But before we can use any of these language features, we first have to think about how to solve the problem and which algorithm to use. In fact, it is the algorithm that determines which language features and constructs we will subsequently use to solve the problem.

We test our functions by specifying various start and end indices and comparing their return values with those of the corresponding built-in methods. If the results differ, our function is incorrect and the test produces an error.

Since none of our test cases produced an error, it appears that our functions behave as expected.

Interested in the e-book Python Knowledge Building Step by Step: From the Basics to Your First Desktop Application?