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 :: __le__ 
Python :: Python how to use __ne__ 
Python :: object at being output python 
Python :: NumPy resize Example out of bound values [appending zeros] 
Python :: NumPy bitwise_and Example When inputs are Boolean 
Python :: selenium rotate user agent 
Python :: python multiply function with return keyword 
Python :: Create a list of multiples of 3 from 0 to 20. 
Python :: NumPy packbits Code Packed array along axis 1 
Python :: django view - mixins and GenericAPIView (retrieve, update or delete - GET, PUT, DELETE) 
Python :: free konta mc 
Python :: simple tower of hanoi project python with gui 
Python :: pandas dataframe limit rows by col value 
Python :: Remove Brackets from List Using join method with loop 
Python :: how to swap a lowercase character to uppercase in python 
Python :: beautifulsoup - extracting link, text, and title within child div 
Python :: python Tkinter widget displacement with pack() 
Python :: write an empty block python 
Python :: decoding to str: need a bytes-like object, list found 
Python :: django insert data into database without form 
Python :: How to solve import errors while trying to deploy Flask using WSGI on Apache2 
Python :: _tkinter.TclError: invalid command name ".!canvas" 
Python :: Frog Jump time complexity 
Python :: ring retrieves the list of all algorithms supported by Encrypt()/Decrypt() functions. 
Python :: ring Trace Library 
Python :: Python soma números 
Python :: weigted average in pandas 
Python :: django date grater 
Python :: ticklabels are not centered heatmap 
Python :: Flask - store object directly in a session [duplicate] 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =