Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

__mul__

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

class Test:
    def __init__(self, x):
        self.x = x
    
    def __mul__(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 multiply 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 multiply these two objects together.
print(t * t2)	# 10, without __mul__ the object doesn't understand the "*" sign.
Comment

PREVIOUS NEXT
Code Example
Python :: raise exception without traceback python 
Python :: how to sort the order in multiple index pandas 
Python :: how to get session value in django template 
Python :: removing duplicates using json python 
Python :: python if not 
Python :: how to set python path in mac 
Python :: blur an image in python 
Python :: python internship 
Python :: sumof product 1 
Python :: switch case dictionary python 
Python :: python dictionary key in range 
Python :: chr() function in python 
Python :: python if boolean logic 
Python :: k fold cross validation from scratch python 
Python :: decision tree python 
Python :: %d%m%Y python 
Python :: python split at index 
Python :: how to find python path 
Python :: run python script inside bash script 
Python :: function with args* example 
Python :: string.format() with {} inside string as string 
Python :: matplotlib limit number of ticks 
Python :: qpushbutton clicked 
Python :: how to create tkinter window 
Python :: Split the string using the separator 
Python :: return python meaning 
Python :: read dict txt python 
Python :: normalize function 
Python :: linear regression python code 
Python :: perform zero crossing using openCV 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =