Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!
We have gone from the original "Hello World" to the object-oriented in the first two lectures. Let's look back and see if there is anything missing in our quick tutorial.
We mentioned one sentence before, "Everything is object". Then we will try this sentence in depth.
First, we will introduce two built-in functions,Dir ()AndHelp ()
Dir () is used to query attributes (variable attributes and method attributes) contained in a class or object ). You can try
>>> Print Dir (list)
Help () is used to query instructions. You can try
>>> Print help (list)
(List is a built-in class in Python, corresponding to the table we have explained before)
1.List is a class
As shown above, the table is a class defined by python. When we create a new table, for example:
>>> NL = [1, 2, 5, 3, 5]
In fact, NL is an object of the List class.
Let's first experiment with some list methods.
>>> Print NL. Count (5)# Count to see the total number of 5
>>> Print NL. Index (3)# Query the subscript of the first 3 in NL
>>> NL. append (6)# Add a new element 6 at the end of NL
>>> NL. Sort ()# Sorting NL Elements
>>> Print NL. Pop ()# Remove the last element from NL and return the element.
>>> NL. Remove (2)# Remove the first 2 from NL
>>> NL. insert (0, 9)# Insert 9 at the position marked as 0
Listing so many items is to let everyone have a concept of list as a class.
(At the end of the course, I will provide an appendix to a common method. If you are interested, you can check it now. There are a wealth of resources available on the Internet)
2. Read through list:Operator is a special method
When you use Dir (list), you can see an attribute, Which is _ add __. In the form, yesSpecial method (underline, underline)Where is it special?
This method defines the meaning of the "+" operator for the list, that is, the operation performed when you add a list object to another list object.
>>> Print [1, 2] + [5, 6, 9]
OperatorSuch as +,-,>, <, and subscript reference [start: end] are basically defined inside the class.Method.
Let's try it first.
>>> Print [1, 2, 3]-[3, 4]
There is an error message indicating that the operator "-" is not defined.
Now we inherit the List class and add the definition "-"
Class Superlist (list ): Def _ Sub __ (Self, B): = Self [:]# Here, self is the object of supelist. Because superlist inherits from 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])
(Description: built-in functionsLen ()Returns the total number of elements contained in the list)
The built-in function _ Sub _ () defines the "-" Operation: remove the elements in the second table from the first table.
(At the end of the tutorial,Special Method)
(If _ Sub _ () has been defined in the parent class and you have defined it in the subclass, The subclass object will refer to the definition of the subclass, instead of loading the definition of the parent class. This is also true for any other attributes .)
Defining operators are very useful for complex objects. For example, humans have multiple attributes, such as name, age, and height. We can define the comparison between humans (>,<,=) as only looking at age. In this way, you can add non-existing operations to the object based on your own purposes.
3. Now, you probably already have a basic concept for python. This is also the purpose of the basic part of this quick tutorial. You may be eager to write someProgramExercise. I believe this will be very good for you. But please note that. Python is powerful because it provides manyReady-to-use objects. We have seen the built-in functions such as list and tuple. They are easy to use. In the standard library of Python, there are 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 that define richer objects, such as numpy, tkinter, and Django, for scientific computing and GUI development, the Web Development Library defines various objects. For general users, it is much easier to use these libraries than to start from scratch (on the shoulders of giants ).
I will try to involve each database later. Thank you for your continued attention.
Welcome to the world of Python.
Summary:
Len () Dir () Help ()
The previously mentioned data structure list (table) is actually an object with multiple attributes.
Operator is a method
Standing on the shoulders of giants