1 description
The Dir () function can view (print) the properties and methods of an object. The type (data, module) has its own properties and methods, regardless of the object (all objects in Python).
The dir () function returns a list of the types of variables, methods, and definitions in the current scope, without parameters;
With parameters, returns the properties of the parameter, a list of methods.
If the parameter contains method __dir__ (), the method is called. If the parameter does not contain __dir__ (), the method will collect the parameter information to the fullest extent.
2 syntax
Dir (object)
- Object--objects, variables, types.
3 return value
Returns a list of properties for an object
4 Example 4.1 Gets the list of properties for the current module
>>> dir () ['__builtins__'__doc__' __loader__'__name__'__package__ '__spec__'a']
4.2 Ways to get a list
>>>dir ([]) ['__add__','__class__','__contains__','__delattr__','__delitem__','__dir__','__doc__','__eq__','__format__','__ge__','__getattribute__','__getitem__','__gt__','__hash__','__iadd__','__imul__','__init__','__iter__','__le__','__len__','__lt__','__mul__','__ne__','__new__','__reduce__','__reduce_ex__','__repr__','__reversed__','__rmul__','__setattr__','__setitem__','__sizeof__','__str__','__subclasshook__','Append','Clear','Copy','Count','Extend','Index','Insert','Pop','Remove','Reverse','Sort']
>>> a = []>>>dir (a) ['__add__','__class__','__contains__','__delattr__','__delitem__','__dir__','__doc__','__eq__','__format__','__ge__','__getattribute__','__getitem__','__gt__','__hash__','__iadd__','__imul__','__init__','__iter__','__le__','__len__','__lt__','__mul__','__ne__','__new__','__reduce__','__reduce_ex__','__repr__','__reversed__','__rmul__','__setattr__','__setitem__','__sizeof__','__str__','__subclasshook__','Append','Clear','Copy','Count','Extend','Index','Insert','Pop','Remove','Reverse','Sort']
The results of both methods are the same as those for viewing the list of actions and properties.
4.3 Viewing the module's properties, methods
>>>ImportTime#Import Module>>> Dir (Time)#View Modules['Clock_monotonic','Clock_monotonic_raw','clock_process_cputime_id','Clock_realtime','clock_thread_cputime_id','_struct_tm_items','__doc__','__loader__','__name__','__package__','__spec__','Altzone','Asctime','Clock','Clock_getres','Clock_gettime','Clock_settime','CTime','Daylight','Get_clock_info','gmtime','localtime','Mktime','monotonic','Perf_counter','Process_time','Sleep','strftime','Strptime','Struct_time',' Time','TimeZone','Tzname','Tzset']
Python Learning note 011--built-in function dir ()