Difference between dir(…) and vars(…).keys() in Python?

前端 未结 3 2141
余生分开走
余生分开走 2020-12-07 08:59

Is there a difference between dir(…) and vars(…).keys() in Python?

(I hope there is a difference, because otherwise this would break the \"

3条回答
  •  感动是毒
    2020-12-07 09:36

    Apart from Answers given, I would like to add that, using vars() with instances built-in types will give error, as instances builtin types do not have __dict__ attribute.

    eg.

    In [96]: vars([])
    ---------------------------------------------------------------------------
    
    TypeError Traceback (most recent call last)
     in ()
          ----> 1 vars([])
    TypeError: vars() argument must have __dict__ attribute
    

提交回复
热议问题