Python informer

Improve your Python coding skills

Magic methods

Python makes extensive use duck typing. This means that when we deal with objects in our code, we are often less concerned with what type of object it is, we really only need to know if it can do certain things. Under the hood, Python uses various magic methods to implement duck typing. These methods have special names - the begin and end in double underscores, for example __len__. For this reason they are sometimes called dunder methods (double underscore, geddit? Read more →

String conversion

As you probably know, you can use the builtin str function to convert any object into a string representation: str(5 + 1) # Returns string '6' str([0]*3) # Returns string '[0, 0, 0]' print(5 + 1) # Displays 6 When you call the print function, it converts the object to a string in a similar way. If you create your own class, you can control how it is converted to a string by defining a __str__ function. Read more →