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__']
  • 相关阅读:
    js原型、原型链、继承的理解
    实用的Object操作方法
    数组操作方法汇总
    ES6数组去重、字符串去重
    ES6新增数据结构:Set和Map
    canvas图片、文字在移动端显示模糊问题
    Tabindex
    javascript 单元测试初入门
    ng-file-upload(在单文件选择,并且通过点击“上传”按钮上传文件的情况下,如何在真正选择文件之前保留上一文件信息?)
    如何优化表单验证
  • 原文地址:https://www.cnblogs.com/keke-xiaoxiami/p/8459386.html
Copyright © 2011-2022 走看看