Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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

(I hope there is a difference, because otherwise this would break the "one way to do it" principle... :)

like image 444
Eric O Lebigot Avatar asked Jun 11 '09 09:06

Eric O Lebigot


People also ask

What does vars () do in Python?

The var() function is part of the standard library in Python and is used to get an object's _dict_ attribute. The returned _dict_ attribute contains the changeable attributes of the object. This means that when we update the attribute list of an object, the var() function will return the updated dictionary.

What does dir () mean in Python?

Python dir() Function The dir() function returns all properties and methods of the specified object, without the values. This function will return all the properties and methods, even built-in properties which are default for all object.

What is __ DIR __ Python?

Python's __dir__() magic method implements the functionality of the dir() built-in function. Semantically, dir() returns all (function, object, or variable) names in a given scope. However, the magic method __dir__() converts any return value to a sorted list. Minimal Example.

What is difference between help and Dir in Python?

Originally Answered: What is the usage of help() and dir() function in Python? dir([object]) returns all attributes of the object. help(object) generate the help of the given object.


1 Answers

Python objects usually store their instance variables in a dictionary that belongs to the object (except for slots). vars(x) returns this dictionary (as does x.__dict__). dir(x), on the other hand, returns a dictionary of x's "attributes, its class's attributes, and recursively the attributes of its class's base classes."

When you access an object's attribute using the dot operator, Python does a lot more than just look up the attribute in that objects dictionary. A common case is when x is an instance of class C and you call its method m:

class C:     def m(self):         print("m")      x = C() x.m() 

The method m is not stored in x.__dict__. It is an attribute of the class C.

When you call x.m(), Python will begin by looking for m in x.__dict__, but it won't find it. However, it knows that x is an instance of C, so it will next look in C.__dict__, find it there, and call m with x as the first argument.

So the difference between vars(x) and dir(x) is that dir(x) does the extra work of looking in x's class (and its bases) for attributes that are accessible from it, not just those attributes that are stored in x's own symbol table. In the above example, vars(x) returns an empty dictionary, because x has no instance variables. However, dir(x) returns

['__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__',  'm'] 
like image 66
M.J. Avatar answered Oct 12 '22 22:10

M.J.