Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

__truediv__

"""
Magic method __truediv__ is called when a "/" is used on any object. 
Given example shows what I mean: print(some_object / other_object)
This may give error often, so that means given objects don't have __truediv__ method
"""

class Test:
    def __init__(self, x):
        self.x = x
    
    def __truediv__(self, other):
    	""" 
    	self represents the object that is first and other is object after
    	"/" sign. What I say here is that I expect to have attribute "x" 
    	in both objects and I will try to divide them. This SHOULD BE
    	used for objects of the same type.
    	"""
    	return self.x / other.x

t = Test(5)
t2 = Test(2)	# Now I can divide these two objects.
print(t / t2)	# 2.5, without __truediv__ the object doesn't understand the "/" sign.

Comment

PREVIOUS NEXT
Code Example
Python :: model compile keras 
Python :: Python __div__ magic method 
Python :: Python how to use __ge__ 
Python :: find max in for scartch python 
Python :: pyqt log widget thread safe 
Python :: NumPy bitwise_and Example When inputs are Boolean 
Python :: simpy 
Python :: make all subplots same height 
Python :: NumPy bitwise_xor Code When inputs are numbers 
Python :: django disable foreign key checks temporary 
Python :: django filter empty onetoone exists 
Python :: after logout using back button is letting it use the flask application 
Python :: if not isinstance multiple values 
Python :: geopandas gdf or df to file 
Python :: get token eth balance python 
Python :: unauthorized vue django rest framework 
Python :: list x[:-1] 
Python :: vscode show when variable is protected or private python 
Python :: make python present number in sciencetifc 
Python :: integration test python 
Python :: how to seperate the script from html template when using jQuery in flask 
Python :: typing effect in python 
Python :: connect labjack to python 
Python :: ring add new items to the list using the string index 
Python :: candelstick chart matplotlib 
Python :: Python loop aray 
Python :: element wise mean and std 
Python :: unable to access jupiter assertions 
Python :: How to check whether a nested hash element exists in python 
Python :: python making player inventory 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =