Python2 why it is best to add object when you are defining a class, without taking it __python

Source: Internet
Author: User
#-.-Coding:utf-8-.-
# __author__ = ' Zhengtong '


class Person: ' "
    without Object" ""
    name = " Zhengtong "


class Animal (object):" "
    with Object
    " ""
    name = "Chonghong"

if __name__ = = "__ Main__ ':
    x = person ()
    print ' person ', dir (x)

    y = Animal ()
    print "Animal", dir (y)

Run results

person [' __doc__ ', ' __module__ ', ' name ']
Animal [' __class__ ', ' __delattr__ ', ' __dict__ ', ' __doc__ ', ' __format__ ', ' __getattribute__ ', 
' __hash__ ', ' __init__ ', ' __module__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', 
' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' __weakref__ ', ' name '

The person class is clearly able to see the difference, not inheriting object, only Doc, module, and its own defined name variable, which means that the namespace of this class has only three objects to manipulate.
The animal class inherits object objects and has many operable objects, which are advanced in the class.

These advanced features are largely useless for students who don't know much about python, but for those who want to write frameworks or write large projects, these features are more useful, such as tornado, where the exception capture is useful to class to locate the name of the class. There is also a high degree of flexibility to pass the parameters of the time to use Dict to complete.

Finally, the article is based on the Python version 2.7.10, which actually has the default in Python 3 for you to load object (even if you don't write object).


Source: https://blog.csdn.net/w571523631/article/details/55099385

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.