Use the Python help function helps (), dir (), type ()

Source: Internet
Author: User

From the initial "Hello world", go to the object-oriented. It's time to look back and see if anything is missing from the tutorial.

We mentioned a word before, "Everything is Object". So let's take a closer look at this sentence.

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 2015, 22:43:06) [MSC v.1600 bit (Intel)]
on Win32 Type "copyright", "cred Its ' or ' license () for the more information.
>>> dir (list)
[' __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 ']
>>> Help (List.pop)
Help on Method_descriptor:

pop (...)
    L.pop ([index])-> item--Remove and return item at index (default last).
    Raises indexerror if list is empty or index.

>>> type (list)
<class ' type ' >
>>> type (list.pop)
<class ' Method_ Descriptor ' >

First you need to introduce two built-in functions, dir (), Help (), type ()

Dir () is used to query for all properties of a class or object. You can try it.

>>>print dir (list)

Help () A descriptive document to query. You can try it.

>>>print Help (list)

(list is a built-in Python class that corresponds to the list we explained earlier)

type function: Viewing the types of variables

<type ' module ' >
>>> type (json.__name__)

list is a class

On top and see, the table is a class that Python has already defined. When we create a new table, for example:

>>>NL = [1,2,5,3,5]

In fact, NL is an object of the class list.

Experiment with some list methods:

>>>print Nl.count (5) # count to see how many 5 are in total

>>>print Nl.index (3) # query NL's first 3 subscript

>>>nl.append (6) # Add a new element at the end of NL 6

>>>nl.sort () # Sort the elements of NL

>>>print Nl.pop () # Removes the last element from the NL and returns the element.

>>>nl.remove (2) # Remove the first 2 from the NL

>>>nl.insert (0,9) # Insert 9 in subscript 0 position

In short, the list is a class. Each list belongs to that class.

The Python supplement contains an appendix to the list of common methods.

operators are special methods

When you use Dir (list), you can see a property, which is __add__ (). The form is a special method (underline, underline). Where is it special?

This method defines the meaning of the "+" operator for the list object, and the action that occurs when the two-list object is added.

>>>print [1,2,3] + [5,6,9]

operators, such as +,-,,, and subscript references [start:end] and so on, are fundamentally defined within the class.


>>>print [1,2,3]-[3,4]

There is an error message stating that the operator "-" is not defined. Now we inherit the list class and add the definition of "-"

Class Superlist (list):
    def __sub__ (self, b):
        a = self[:] # Here, Self is supelist object. Because Superlist inherits from the list, it can use the same reference method as list[:] to represent the entire object.
        B = b[:] While
        len (b) > 0:
            element_b = B.pop ()
            if Element_b in a:
                a.remove (element_b)
        Return a

print superlist ([1,2,3])-superlist ([3,4])

The built-in function len () is used to return the total number of elements contained in the list. The built-in function __sub__ () defines the action of "-": Remove the element that appears in the second table from the first table. If __sub__ () is already defined in the parent class, and you define it in the subclass, then the object of the subclass refers to the definition of the subclass and does not load the definition of the parent class. The same is true of any other attribute.

(The tutorial will also give a list of special methods at the end)

Defining operators is useful for complex objects. For example, humans have multiple attributes, such as name, age, and height. We can define human comparisons (",", =) to look at age alone. This can be based on their own purposes, the original does not exist to increase the operation of the object.


I hope you've got a basic understanding of Python. You may be tempted to write some programs to practice. It would be good for you.

But a big part of Python's strength is that it provides a lot of already-written, ready-to-use objects. We've seen the built-in list, and tuple and so on. They are very convenient to use. In the Python standard library, there are also a large number of objects that can be used for operating system interaction, Internet development, multithreading, and text processing. On the basis of all these, there are many external library packages, which define richer objects, such as NumPy, Tkinter, Django, etc. for scientific computing, GUI development, web development, and various objects. For the average user, it's much easier to use these libraries than to start from scratch. We're going to start climbing the shoulders of giants.

Thank you for your attention,

Welcome to the Python world.


Len () dir () help ()

A data structure list is a class.

Operators are methods

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: 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.