Python core programming Some of the mistakes in this book

Source: Internet
Author: User
Tags instance method

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.