The second edition of Python core programming, which is much more detailed than the second edition of the Python Basics tutorial, enriches many details, although it is a classic introductory book, but I find there are some obvious mistakes. In this chapter of object-oriented programming, there are two errors
1). It says that any class has some built-in special class attributes (that is, programmers are not defined in the class), see
2). It says that the __new__ method is more like a class constructor than the __init__ method. See:
The following tests are performed:
1 #Encoding:utf-82 classMyClass ():3 defDoprint (self):4 Print 'Doprint invoked.'5 6 def __init__(self):7 Print '__init__ method invoked.'8 9 def __new__(self):Ten Print '__new__ method invoked.' One A def __test__(self): - Print '__test__ method invoked.' - the classMyClass2 (): - Pass - - if __name__=='__main__': + Printdir (MyClass2) - PrintMyClass2.__dict__ + AMc=MyClass () atMc.__test__() -Mc.__new__()
Output Result:
['__doc__'__module__']{'__module_ _'__main__'__doc__': None}__init__ method invoked. __test__ method invoked. __new__ method invoked.
By analyzing the test results you can find:
for 1), by default, the class does not have so many special attributes.
for 2), by default, none of the classes contains the __new__ method (dir (MyClass2)), and even if the programmer explicitly defines the method, the class does not invoke it at instantiation, but only calls the __init__ method, __new__ Method exists only as a normal instance method and only works when explicitly invoking it.
Test Platform for Windows7,python2.7.11
Python core programming Some of the mistakes in this book