Python uses the Dir function to see a description of all the members in a class

Source: Internet
Author: User
Tags builtin
This article mainly introduces you about how Python uses the Dir function to see all the member functions in the class, the text of the sample code introduced in very detailed, for everyone to learn or use Python has a certain reference learning value, the need for friends below with the small series to learn to study under.

Objective

If a class is written by someone else and does not have a help document, how do you see all the member functions? This article gives you a detailed introduction to Python with the Dir function to see all the member functions in the class, the following words do not say, come together to see the detailed introduction.

You can use the following code:


# file:builtin-dir-example-2.py  Class A:  def a (self):   pass  def b (self):   Pass  Class B (A):  def c (self):   pass  def d (self):   pass  def getmembers (Klass, Members=none):  # Get a list of all Class members, ordered by class  if members are None: Members   = [] for  K in klass.__bases__:   getmembers ( K, members) for  M in Dir (Klass):   if M not in Members:    members.append (m)  return to Members  print (' A=>: ', GetMembers (A) print () print (' b=>: ', GetMembers (B)) print (' ioerror=>: ', GetMembers (IOError))

The output results are as follows:


>>> = = restart:d:/work/csdn/python_game1/example/builtin-dir-example-2.py ====a=>: [' __class__ ', ' __ Delattr__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __init_subclass__ ', ' __le__ ', ' __lt__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr_ _ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' __dict__ ', ' __module__ ', ' __weakref__ ', ' A ', ' B ']b=>: [' __class__ ' , ' __delattr__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' __gt__ ', ' __hash__ ', ' __ Init__ ', ' __init_subclass__ ', ' __le__ ', ' __lt__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __ Setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' __dict__ ', ' __module__ ', ' __weakref__ ', ' A ', ' B ', ' C ', ' d '] ioerror=>: [' __class__ ', ' __delattr__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __init_subclass__ ', ' __le__ ', ' __lt__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str __ ', ' __subclasshook__ ', ' __cause__ ', ' __context__ ', ' __dict__ ', ' __setstate__ ', ' __suppress_context__ ', ' __traceback __ ', ' args ', ' with_traceback ', ' characters_written ', ' errno ', ' filename ', ' filename2 ', ' strerror ', ' Winerror ']>> >

In this example, the member function of the output base class A is exported, and the member function of the derived class B is output.

Dir () built-in function functions

Python has a lot of built-in methods, and the Dir () function is very useful when both beginners and pass-through Python programmers can't remember all the methods. With the Dir () function you can see all of the methods within the image, in Python everything is a pair of like, a data type, a module, etc., all have their own properties and methods, in addition to the usual method, you do not need to remember it all, to the Dir () function is good.

How to use the Dir () function

The Dir () function is simple, so you can use it only if you want to query and write to () parentheses.

For example, if you want to see what the list does, you can directly pass in the empty list to a variable name like [] or a list data type, as in the following:


>>>dir ([])

Or


x = [' A ', ' B ']>>>dir (x)


The results of both operations are the same as the way the view lists are manipulated and properties. If you want to look up a string, just put the parameter variable name in () or empty string "on it.

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.