Python Frequently Asked Questions, Release 3.9.5 Chapter 3. Design and History FAQ 38 Finally, for instance variables it solves a syntactic problem with assignment: since local variables in Python are (by definition!) those variables to which a value is assigned in a function body (and that aren’t explicitly declared global), there has to be some way to tell the interpreter that an assignment was meant to assign to an instance variable instead of to a local variable, and it should preferably be syntactic (for efficiency reasons). C++ does this through declarations, but Python doesn’t have declarations and it would be a pity having to introduce them just for this purpose. Using the Second, it means that no special syntax is necessary if you want to explicitly reference or call the method from a particular class. In C++, if you want to use a method from a base class which is overridden in a derived class, you have to use the$_{::}$ operator – in Python you can write$_{baseclass.methodname(self, <argument}$ list>) . This is particularly useful for$_{__init__()}$ methods, and in general in cases where a derived class method wants to extend the base class method of the same name and thus has to call the base class method somehow. First, it’s more obvious that you are using a method or instance attribute instead of a local variable. Reading$_{self.x}$ or$_{self.meth()}$ makes it absolutely clear that an instance variable or method is used even if you don’t know the class definition by heart. In C++, you can sort of tell by the lack of a local variable declaration (assuming globals are rare or easily recognizable) – but in Python, there are no local variable declarations, so you’d have to look up the class definition to be sure. Some C++ and Java coding standards call for instance attributes to have an$_{m_}$prefix, so this explicitness is still useful in those languages, too. The idea was borrowed from Modula-3. It turns out to be very useful, for a variety of reasons. Another advantage is that strings in Python are considered as “elemental” as numbers. No amount of activity will change the value 8 to anything else, and in Python, no amount of activity will change the string “eight” to anything else. One is performance: knowing that a string is immutable means we can allocate space for it at creation time, and the storage requirements are fixed and unchanging. This is also one of the reasons for the distinction between tuples and lists. There are several advantages. For a fuller explanation, please see the floating point arithmetic chapter in the Python tutorial. The typical precision of 53 bits provides Python floats with 15–16 decimal digits of accuracy. which is exactly: the value stored for$_{x}$ is a (very good) approximation to the decimal value$_{1.2}$ , but is not exactly equal to it. On a typical machine, the actual stored value is: Many numbers that can be written easily in decimal notation cannot be expressed exactly in binary floating-point. For example, after: The$_{float}$type in CPython uses a C$_{double}$for storage. A$_{float}$object’s value is stored in binary floating-point with a fixed precision (typically 53 bits) and Python uses C operations, which in turn rely on the hardware implemen- tation in the processor, to perform floating-point operations. This means that as far as floating-point operations are concerned, Python behaves like many popular languages including C and Java. >>> x =1.2 (binary) (decimal) 3.5 Why must ‘self’ be used explicitly in method definitions and calls? 3.4 Why are Python strings immutable?