zoukankan      html  css  js  c++  java
  • 【Python深入】Python中继承object和不继承object的区别

    python中定义class的时候,有object和没有object的不同?例如:

    class Solution(object):

    class Solution():

    这俩的区别在于——————

    在python2.x中,通过分别继承自object和不继承object定义不同的类,之后通过dir()和type分别查看该类的所有方法和类型:

    >>> class test(object):
    ...     pass
    ...
    >>> dir(test)
    ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '_
    _init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__size
    of__', '__str__', '__subclasshook__', '__weakref__']
    >>> type(test)
    <type 'type'>
    >>> class test2():
    ...     pass
    ...
    >>> dir(test2)
    ['__doc__', '__module__']
    >>> type(test2)
    <type 'classobj'>

    在3.x中:两者是一致的,因为在3.x中,默认继承就是object了

    >>> class test(object):
        pass
    
    >>> class test2():
        pass
    
    >>> type(test)
    
    <class 'type'>
    
    >>> type(test2)
    
    <class 'type'>
    
    >>> dir(test)
    
    ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
    
    >>> dir(test2)
    
    ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
  • 相关阅读:
    Python中使用PyMySQL
    python实现通用json导入到mysql
    python实现通用excel导入到mysql
    java优化几个小步骤
    日志添加request-id
    Nginx优化
    tomcat8 性能优化参考
    excel空格处理
    spring boot swagger ui使用 nginx 部署后无法使用问题
    Swagger2多包扫描
  • 原文地址:https://www.cnblogs.com/keke-xiaoxiami/p/8459386.html
Copyright © 2011-2022 走看看