

The list function is also a handy way to make a "factory function" that creates a new empty list. That's by far the most handy reason to use list and I very often see folks forget that list is a great tool for exactly this purpose! Inherited class is creating a new instance variable instvar1. Those first three reasons to use list are all about taking an old iterable and making a new list out of it. In the above example, we are having a constructor in base and inherited class.

This use case for list is really just a variation of the last two, but this is an important enough concept that it's worth repeating in 3 different ways! Using list as a factory function

In fact, many new Pythonistas might assume that readlines would return an iterator ( many built-in functions return iterators after all). So it is not the only method for the constructor. With my_file.readlines(), it's not quite as obvious that we're creating a list of lines. Many beginners and even Pythonic Programmers think that init () is a constructor as it initializes values when it is called, but init () is a special method which initializes objects and it is immediately called when an object is created and initialized and does not allocate memory. With list(my_file), it's clear that we're looping over a file and storing each line in a new list (assuming you know that looping over a file provides lines). If I wanted to create a class, the constructor of which can take, suppose, the value of a and b using kwargs but if nothing is passed while creating the object, I want a and b to take default val.
