Python Learning note 011--built-in function dir ()

Source: Internet
Author: User

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 ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.