Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

__floordiv__

"""
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 :: how to do tail recursion in python 
Python :: pandas fillna with mode 
Python :: generate a random np image array with shape 
Python :: reverse order of dataframe rows 
Python :: binary search iterative python 
Python :: Python __add__ magic method 
Python :: keras embedding 
Python :: python crosshair overlay 
Python :: add space before and after string python 
Python :: threshold meaning in pandas dropna 
Python :: np append 
Python :: sorted set in python 
Python :: easy python gui 
Python :: s=0 def sum(x,y): n=int(input("enter no. of terms") for i in range(n): l=int(input("enter no.")) s=s+l print(s) sum() 
Python :: python word encode asci 
Python :: print f python 
Python :: python 3 documentation 
Python :: create django model field based on another field 
Python :: decision tree algorithm 
Python :: least recently used cache 
Python :: count number of subdirectories 
Python :: numpy concatenate arrays 
Python :: def create(self validated_data) 
Python :: Merge 2 or more notebooks into one 
Python :: getting last n rows of column 
Python :: how to define the range of values in seaborn heatmap 
Python :: python reading into a text file and diplaying items in a user friendly manner 
Python :: mid point circle drawing 
Python :: rest plus 
Python :: rank function in pandas 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =