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 store .png file in variable python 
Python :: crawling emails with python 
Python :: error:pip.subprocessor:command errored out with exit status 1: 
Python :: python ternary statement 
Python :: object function in python 
Python :: __add__ 
Python :: python defaultdict default value 
Python :: flask sqlalchemy case insensitive like 
Python :: detect if usb is plugged in python 
Python :: python get focused window 
Python :: viewset and router 
Python :: Python Deleting a Tuple 
Python :: python if column is null then 
Python :: Converting a HDFDataset to numpy array 
Python :: stores number in set using input in python 
Python :: copy module in python 
Python :: python avg 
Python :: selenium select svg python3 
Python :: random list 
Python :: Object of type datetime is not JSON serializable 
Python :: how to check if some file exists in python 
Python :: python plot normal distribution 
Python :: Exiting from python Command Line 
Python :: Python Zigzag a matrix for dct 
Python :: executing a python script interactively 
Python :: Roberta Inference TensorFlow 
Python :: pyton count senteses in a text file 
Python :: remove rows from a dataframe that are present in another dataframe? 
Python :: python selenium teardown class 
Python :: python remove first element of array 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =