dir |
| dir([object]) |
Returns a list of the attributes and methods of an obj or the names in the current local scope (built-in). |
| object | optional |
| REMARKS |
| * This is a built-in function. * This function tries to return the most relevant, rather than complete, information. * With no argument, this returns the list of names in the current local scope. * With an argument, this returns a list of valid attributes for that object. * If the object has a method named __dir__(), this method will be called and it must return the list of attributes. * If the object is a module object, the list contains the names of the module's attributes. * If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases. * Metaclass attributes are not in the result list when the argument is a class. * For the Official documentation refer to python.org |
dir() # show the names in the module namespace
['__builtins__', '__name__', 'struct']
x = 10
y = 20
def foo():
pass
print(dir())
['__annotations__', '__builtins__', '__doc__', '__loader__',
'__name__', '__package__', '__spec__', 'foo', 'x', 'y']
class Shape:
def __dir__(self):
return ['area', 'perimeter', 'location']
s = Shape()
dir(s)
['area', 'location', 'perimeter']
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top