Python dunder methods documentation

A Guide To Python’s Dunder Methods

Python has several magic methods — you’d typically hear practitioners refer to as dunder methods (I’ll be referring to them as both interchangeably). These methods carry out a procedure known as operator overloading: providing extended meaning beyond the predefined meaning to an operator. We use operational overloading to add custom behaviors to our classes so we can use them with Python’s operators and built-in functions.

The simplest way to think of a dumber method is as a contract between your implementation and the Python interpreter. One of the terms of the contract involves Python performing some actions behind the scenes under some given circumstance (i.e. trying to add an integer to a custom class).

Dunder methods begin and end with two underscores: the most popular one you may come across is the __init__() method. We create the __init__() method in a class so we can allow the class to be initialized with specific attributes of the class — Learn more about Object-oriented programming in Getting Started with Object-Oriented Programming in Python 3.

But __init__() is only one of several magic methods. In this article, we’ll cover the different types of dunder methods you may come across and what they do.

String representation methods

Whenever we create a new object in Python, we implicitly create a related object since all classes inherit from Object . The methods defined in the Object are inherited by our newly created class and are used in various situations like when an object is printed.

class Car: 
pass
car = Car()print(car)"""

"""

How did the code above know what to print? Simple. The Object , which is the parent of all classes in Python, has a dunder method called __repr__() (pronnounced dunder repper); When we call the print() statement it makes a call to the __repr__() method from our Car object, which was inherited from the parent class Object , and returns a value back to the main program.

Читайте также:  Html поле ввода год

But a child calss can override the parent class. All we have to do is create a…

Источник

Day 13 — Dunder Methods¶

“Everything in Python is an object.” You’ve probably heard this before, that’s because it is, every data type we use is an object that has been enriched to increase its performance.

Dunder methods, or magic methods, are methods we can define in our classes to give them more functionality than they have. These methods allow us to simulate the behavior of different data types from those we can find in the language.

The name dunder methods come from double underscores as a prefix and suffix of the method, e.g. __init__ .

class User: def __init__(self, name): self.__name = name new_user = User("Christopher") print(new_user) # Output: #

I’ve created a basic class User which receives the name at the moment of its instantiation. When printing this instance, we can see the address in memory although now this is not what we need, how about improving this?

Object Representation¶

__repr__ is a method that allows us to define what information is going to represent our class, in this case, to see clear information of the created instance.

class User: def __init__(self, name): self.__name = name def __repr__(self): return f"Class User: " new_user = User("Christopher") print(new_user) # Output: # Class User: Christopher 

Count¶

You have probably used the len() function more than once, but did you know we can also implement this in our objects?

class User: def __init__(self, name): self.__name = name def __repr__(self): return f"Class User: " def __len__(self): return len(self.__name) new_user = User("Christopher") print(new_user) print(len(new_user)) # Output: # Class User: Christopher # 11 

The len() function allows us to know the number of characters in a string, however, we can use the magic method __len__ to know from our object the number of characters that our User represents.

More¶

There are quite a few methods that we probably won’t need, however, we can practice with each of them to enrich our code like never before. Dunder Methods

© Copyright 2020-2020, Esteban Solorzano Revision 02e15a03 .

Versions latest Downloads pdf On Read the Docs Project Home Builds Free document hosting provided by Read the Docs.

Источник

Оцените статью