Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python __div__() Magic Method

#1
Python __div__() Magic Method

The Python __div__() magic method overrides the division operation for a custom object in Python 2.

In Python 3, it was replaced by the __truediv__() for a / b and __floordiv__() dunder methods for a // b.

  • The Python __truediv__() method is called to implement the normal division operation / called true division. For example to evaluate the expression x / y, Python attempts to call x.__truediv__(y).
  • The Python __floordiv__() method implements the integer division operation // called floor division. For example to evaluate the expression x // y, Python attempts to call x.__floordiv__(y).

If the method is not implemented, Python first attempts to call __rtruediv__ or __rfloordiv__ on the right operand and if this isn’t implemented either, it raises a TypeError.

TypeError: unsupported operand type(s) for /


In the following example, you try to override the division operator on the custom object Data by using the __div__() magic method.

# Python 3 - WRONG:
class Data: def __div__(self, other): return 42.42 x = Data()
y = Data() print(x / y)

However, this doesn’t work for Python 3—you obtain the following output error message:

Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 9, in <module> print(x / y)
TypeError: unsupported operand type(s) for /: 'Data' and 'Data'

To fix this issue, override the __truediv__() magic method for Python 3 instead of the __div__() magic method for Python 2 to define the true division operator x / y.

You can see how it’s done in the following code example (see highlighted lines):

class Data: def __truediv__(self, other): return 42.42 x = Data()
y = Data() print(x / y)
# 42.42

Explainer Video Division Operators


You can also check out my explainer video where I’ll give you a deep dive on the integer and true division operators and how to use them for various data types. Click to watch:






https://www.sickgaming.net/blog/2021/11/...ic-method/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

[-]
Discord

[-]
Active Threads
News - Video: We Have A Lot Of Feelings ...
Last Post: xSicKxBot
Yesterday 11:53 PM
» Replies: 0
» Views: 14
News - Iconic Rockstar And Actor Meat Lo...
Last Post: xSicKxBot
Yesterday 11:53 PM
» Replies: 0
» Views: 1
(Indie Deal) FREE Last Horizon, Microids...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 10
Microsoft - Celebrating 20 years of Trus...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 3
Xbox Wire - New EA Play Member Rewards F...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 3
News - PS5 Will Outsell Series X|S 2:1 I...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 4
[Tut] Pandas get_dummies() – A Simple Gu...
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 19
Mobile - Genshin Impact’s Tomo – vision,...
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 5
News - Community Focus: Parris
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 8
News - Bedrock: BETA 1.18.10.28
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 6

[-]
Twitter



Discord Server © SickGaming.net 2012-2021