__dict__:要是对象的话返回的是一个对象自身的实例属性、不包括类的属性;要是类的__dict__则不包括父类的属性,只包含自身类属性【方法、类变量】,不包括实例属性。正是这样、每个实例的实例属性才会互不影响。

dir():返回的是对象的所有属性、包括父类的属性

python一切皆对象、类本身也有__dict__函数、跟dir()函数、那么类与实例的两个函数有没有区别呢?

答案是:有的

  1. class A():
  2. Tang = "tang"
  3. def __init__(self):
  4. self.model = None
  5. self.setModel()
  6.  
  7. def setModel(self):
  8. self.model = "tanglaoer"
  9.  
  10. class B(A):
  11. BLao = "BLao"
  12. def __init__(self):
  13. self.model = "hello world"
  14. super().__init__()
  15.  
  16. b = B()
  17. print(b.__dict__)
  18. print(B.__dict__)
  19. print(A.__dict__)
  20. print(dir(b))
  21. print(dir(B))
  22. print(dir(A))

打印结果

  1. {'model': 'tanglaoer'}
  2. {'__module__': '__main__', 'BLao': 'BLao', '__init__': <function B.__init__ at 0x1053c1378>, '__doc__': None}
  3. {'__module__': '__main__', 'Tang': 'tang', '__init__': <function A.__init__ at 0x1053c1488>, 'setModel': <function A.setModel at 0x1053c1510>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None}
  4. ['BLao', 'Tang', '__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__', 'model', 'setModel']
  5. ['BLao', 'Tang', '__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__', 'setModel']
  6. ['Tang', '__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__', 'setModel']

python __dict__ 跟 dir()的区别的更多相关文章

  1. python中的__dict__和dir()的区别

    Python下一切皆对象,每个对象都有多个属性(attribute),Python对属性有一套统一的管理方案. __dict__与dir()的区别: dir()是一个函数,返回的是list: __di ...

  2. __dict__和dir()的区别

    __dict__和dir()的区别 dir() 一般用来查看模块的属性 __dict__从某方面上来说是dir()的子集 可以直接打印dir(),显示的是当前执行文件所有的属性 __dict__ __ ...

  3. python中__dict__与dir()的区别

    在python中__dict__与dir()都可以返回一个对象的属性,区别在于: __dict__是对象的一个属性,而dir()是一个built-in的方法: __dict__返回一个对象的属性名和值 ...

  4. python之__dict__与dir(转载)

    Python下一切皆对象,每个对象都有多个属性(attribute),Python对属性有一套统一的管理方案. __dict__与dir()的区别: dir()是一个函数,返回的是list: __di ...

  5. Python 由__dict__和dir()引发的一些思考

    关于__dict__和dir()的区别和作用请参考这篇文章:http://blog.csdn.net/lis_12/article/details/53521554 说下我当时遇到的问题: class ...

  6. python中类与对象的命名空间(静态属性的陷阱)、__dict__ 和 dir() 在继承中使用说明

    1. 面向对象的概念 1)类是一类抽象的事物,对象是一个具体的事物:用类创建对象的过程,称为实例化. 2)类就是一个模子,只知道在这个模子里有什么属性.什么方法,但是不知道这些属性.方法具体是什么: ...

  7. __dict__和dir()的区别:未完

    1.  dir()是一个函数,返回的是list.__dict__是一个字典,键为属性名,值为属性值: 2.  dir()用来寻找一个对象的所有属性,包括__dict__中的属性,所以说__dict__ ...

  8. ava、Python和PHP三者的区别

    Java.Python和PHP三者的区别 2017年07月15日 22:09:21 书生_AABB 阅读数:18994   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...

  9. python中// 和/有什么区别

    python中// 和/有什么区别 通常C/C++中,"/ " 算术运算符的计算结果是根据参与运算的两边的数据决定的,比如: 6 / 3 = 2 ; 6,3都是整数,那么结果也就是 ...

随机推荐

  1. (十五)C语言之字符串

  2. Linux 常用命令之df du

    1.du 命令:显示每个文件或目录的磁盘使用空间 1) du -h --max-depth [root@ip101 app]# pwd /opt/app [root@ip101 app]# du -h ...

  3. laravel 先orderBY再groupby,导致分组后的排序不正确

    //联系过我的经纪人 $appletChats=$this->AppletChat->orderBy('created_at','desc')->where([['user_id', ...

  4. ubuntu下编译linux内核之前需要做哪些准备?

    答: 安装必要的工具(笔者使用的ubuntu代号为bionic) sudo apt-get install -y bison flex

  5. LC 656. Coin Path 【lock, Hard】

    Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...

  6. 设置placeholder 颜色

    ::-webkit-input-placeholder { /* WebKit browsers */ color: rgb(100, 193, 173); } :-moz-placeholder { ...

  7. java.sql.SQLException: Listener refused the connection with the following error: ORA-12505, TNS:list

    package DisplayAuthors; import  java.sql.*; public class DisplayAuthors  { private static final  Str ...

  8. Servlet 表单数据 接收get post 参数实例

    Servlet 表单数据 很多情况下,需要传递一些信息,从浏览器到 Web 服务器,最终到后台程序.浏览器使用两种方法可将这些信息传递到 Web 服务器,分别为 GET 方法和 POST 方法. GE ...

  9. VMWare虚拟机->锁定文件失败,打不开磁盘的解决办法

    VMWare虚拟机提示:锁定文件失败,打不开磁盘的解决办法   如果使用VMWare虚拟机的时候突然系统崩溃蓝屏,有一定几率会导致无法启动,会提示:“锁定文件失败,打不开磁盘...或它所依赖的某个快照 ...

  10. xaml 中 引用嵌套类的对象

    public class MotorType1 { public class MotorType2 { public class MotorType3 { /// <summary> // ...