
Quark's Outlines: Python Coercion Rules
Quark’s Outlines: Python Coercion Rules Overview, Historical Timeline, Problems & Solutions An Overview of Python Coercion Rules What are Python coercion rules? When you use a binary operator like + , - , or * in Python, Python must decide how to combine two values. The values may come from different types. The process Python uses to find a common type or method is called coercion . You can think of Python coercion rules as a way to keep the program moving when two types meet. Python checks if one object knows how to handle the operation. If not, it checks if the other object knows. In special cases, Python lets objects transform themselves to work better with others. Python lets objects work together using coercion rules. print ( 3 + 5.0 ) # prints: # 8.0 Here Python turns the integer 3 into a float so it can add it to 5.0 . This is automatic coercion. How do Python coercion rules handle special cases? When you mix user-defined objects, Python gives your class a chance to control how
Continue reading on Dev.to Tutorial
Opens in a new tab




