There is a built-in function in python that is versatile. It can be used to measure the number of items or length of any object. That function is the python length function. It is denoted as len(s). It is a quintessential tool. I have found it useful on so many occasion and I believe you have been using it without giving a second thought to how important it is to your programming work. In this post, I will describe the objects it can be used for and some of the benefits of the python length function.
The syntax of the python length function.
The syntax of the built-in python length function is len(s)
where s, the argument, can be any sequence or a collection. A sequence as you must know is any object that is either a string, byte, tuple, list, or range. A collection can be either a dictionary, set, or frozen set. We will be showing how the length function can be used for each of these. As we already are aware, the function returns the number of items of an object passed to it, but if you fail to pass it an object or you pass it an invalid argument, it will return a TypeError.
I have noticed that many beginners associate a sequence with python lists and so they think that the python length function only calculates python list size. Well, in these examples, I want you to think of other objects as sequences.
Examples of its use on sequences.
Here are some examples of its use on sequences and collections.
- First, on strings and byte objects.
- Also, on lists, tuples, and range, see how it works.
- And finally on collections like python dictionaries, sets, and frozensets.
Notice that they correctly returned the same number of items when the len function was called on them.
You will notice that I was converting from the dictionary to set and frozensets. I wanted the examples to be correspondent. Note that frozensets are immutable while sets are mutable.
Now, let’s go to the application of the python length function. That’s the fun part.
Application of the python length function.
There are several uses of the python length function. As we have already described, it gives the length of an object. But its usefulness gives performance optimization when you are writing code. In the examples above, I give instances where the object length is extremely useful and how the len function is used in those instance.
- Used as argument to the range function.
- When object length is required for conditionals.
When using the range function you need to pass it an integer argument for it to compute a range. When you want to iterate over the items in a sequence and this is based on the length of the sequence, then the len function comes in handy to provide the integer argument that the range needs. The range items can then be used as indices to the sequence. Let’s show with an example:
There are times you want to compare objects based on their length. The python length function comes in handy in this case. Here is an example.
These are two common examples I have seen in code where the python length function is widely used.
Using the python length function in user defined objects.
Often, we might want to use the len function to find out the number of items a user defined object contains, especially when the underlying data structure of the user defined object is a sequence. You need to implement the __len()__ special method to be able to do this. When you call len(sequence)
on an object, under the hood, it calls the __len__() special method of that object. So, this gives us the ability to just define a __len__() method for our user defined objects and they would behave like sequences and collections when it comes to using the python length function.
So, here is some code.
In the Fruits class, I set the initial items to be the empty list, that is, when a fruit object is instantiated, it does not contain any fruit. Then, we need to add fruits to the basket. Before adding fruits, I defined an add_fruit method which first checks that the fruit you are adding is an instance of a string. All fruit names are strings by default. Then if that comes through, I add the fruit to the list of items. Then, we implemented the __len__() special method in order to calculate the length of the list after fruits are added. Without implementing this special method, we could not use the python length function with the f1 object which is an instance of the Fruits class.
One way of thinking about the built-in python length function is that it is calling on the implementation of the __len__() special method of the object passed as argument. You could think of it as acting as this:
def len(x):
return x.__len__()
I believe you have all you need to use the quintessential python length function. Be creative. Go write some code using this python length function.
No comments:
Post a Comment
Your comments here!