Why does Python use ‘magic methods’?

AFAIK, len is special in this respect and has historical roots. Here’s a quote from the FAQ: Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))? The major reason is history. Functions were used for those operations that were generic for a group of types and which were … Read more

What is the __dict__.__dict__ attribute of a Python class?

First of all A.__dict__.__dict__ is different from A.__dict__[‘__dict__’]. The former doesn’t exist and the latter is the __dict__ attribute that the instances of the class would have. It’s a data descriptor object that returns the internal dictionary of attributes for the specific instance. In short, the __dict__ attribute of an object can’t be stored in … Read more

PHP __get and __set magic methods

__get, __set, __call and __callStatic are invoked when the method or property is inaccessible. Your $bar is public and therefor not inaccessible. See the section on Property Overloading in the manual: __set() is run when writing data to inaccessible properties. __get() is utilized for reading data from inaccessible properties. The magic methods are not substitutes … Read more