Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python __floordiv__ magic method

"""
Magic method __floordiv__ 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 __floordiv__ method
"""

class Test:
    def __init__(self, x):
        self.x = x
    
    def __floordiv__(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 use floor division on 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 floor divide these two objects.
print(t // t2)	# 2 (5//2 is closest to 2), without __floordiv__ the object doesn't understand the "//" sign.

Comment

PREVIOUS NEXT
Code Example
Python :: pysimplegui start value 
Python :: pandas assign value to row based on condition 
Python :: pandas to_csv hebrew 
Python :: how does a neural network work 
Python :: binary search iterative 
Python :: Python how to use __add__ 
Python :: how to change theme of jupyter notebook 
Python :: WSGIPassAuthorization on 
Python :: spacy create tokenizer 
Python :: unable to import flask pylint 
Python :: python get image RGB data from URL 
Python :: how to send image to template thats not in static flask 
Python :: regex in python 
Python :: reduce dataframe merge 
Python :: add text to jpg python 
Python :: unity python 
Python :: how to install dependencies python 
Python :: How to plot Feature importance of any model in python 
Python :: decision tree 
Python :: string to list 
Python :: string remove ,replace, length in python 
Python :: activate venv environment 
Python :: enum python print all options 
Python :: algebraic pyramid python 
Python :: scrape pdf out of link 
Python :: python np array get dimantion 
Python :: string in netcdf file python 
Python :: django get form id from request 
Python :: pandas append sheet to workbook 
Python :: plotly create plot 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =