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 :: opposite case in python 
Python :: Returns a DataFrame representing the result of the given query 
Python :: customize email for djoser activation 
Python :: python boucle for 
Python :: iterative binary search 
Python :: python __add__ 
Python :: how to encrypt and decrypt strings python 
Python :: fast fourier transform 
Python :: access key through value python 
Python :: git clone in python to tmp directory 
Python :: using polymorphism in python 
Python :: raspberry pi python 
Python :: reverse a number in python 
Python :: keras functional api embedding layer 
Python :: setting python2 in the path for npm install 
Python :: print(f ) python 
Python :: python docs 
Python :: python pip past 
Python :: count unique values in python 
Python :: lru cache 
Python :: how to adda vaslues to data frame 
Python :: # convert string to date 
Python :: separate each characters by commas into a single characters separated by commas 
Python :: 2 functions at a time py 
Python :: fastest way to compute pair wise distances python 
Python :: wordcount pyspark 
Python :: cannot create group in read-only mode. keras 
Python :: mid-point circle drawing 
Python :: 20 minute timer with python 
Python :: iterrows pd 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =