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 :: Python __ge__ magic method 
Python :: __ne__ 
Python :: create different size matplotlib 
Python :: pyqt log widget thread safe 
Python :: NumPy bitwise_and Example When inputs are numbers 
Python :: Open S3 object as string in Python 3 
Python :: get forex exchange rates in python 
Python :: colorbar over two axes 
Python :: NumPy packbits Syntax 
Python :: django view - APIView (urls.py config) 
Python :: http://172.18.0.128:8114/ 
Python :: How to run a method before/after all class function calls with arguments passed? 
Python :: gensim prepare corpus 
Python :: how to initialize a token spacy python 
Python :: raspberry pi set python 3 as default 
Python :: how to remove a strech in pyqt5 
Python :: parsing output from ping - python 
Python :: get distance between points in 1 array pythoin 
Python :: python output 
Python :: ignore exception decorator 
Python :: Flask - how do I combine Flask-WTF and Flask-SQLAlchemy to edit db models 
Python :: cyclic rotation python 
Python :: python return inline if 
Python :: ring load the odbclib.ring library 
Python :: ring Type Hints Library 
Python :: All objects and constants needed to use the ldap3 library can be imported from the ldap3 namespace 
Python :: insertar valor python 
Python :: how to auto create a three dimensional array in python 
Python :: get picamera feed 
Python :: what does // mean in python 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =