Vamei Source: Http://www.cnblogs.com/vamei
Before mentioning a sentence: Everything is object, below we have a deep experience of this sentence
Need to first introduce two built-in functions, dir (), Help ()
Dir () is used to query the properties of a class or object,
>>> print dir (list)
Help () to query the description document
>>> Print help (list)
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.
To experiment with some list methods:
>>>print Nl.count (5) # count, see how many of them in total 5
>>>print Nl.index (3) # Query the subscript of the first 3 of NL
>>>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 NL and returns the element.
>>>nl.remove (2) # Remove the first 2 from NL
>>>nl.insert (0,9) # Insert 9 at position Subscript 0
In summary, list is a class. Each list belongs to that class.
The Python supplement has an appendix to the list of common methods.
Operator is a special method
When using Dir (list), you can see an attribute, which is __add__ (). From the formal perspective is a special method (underline, underline). Where is it special?
This method defines the meaning of the "+" operator for the list object, and the actions that occur when the two list objects are added together.
>>> Print [+] + [5,6,9]
Output [1, 2, 3, 5, 6, 9]
operators, such as +,-,, <, and subscript references [start:end], and so on, are essentially methods defined within the class.
Try
Try
>>>print [+]-[3,4]
There will be 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 the object of the supelist. Since 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 Aprint superlist ([+])-superlist ([3,4])
Class Superlist (list):
def __sub__ (self, b):
A = self[:]
b = b[:]
While Len (b) > 0:
Element_b = B.pop ()
If Element_b in a:
A.remove (Element_b)
Return a
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 elements from the first table that appear in the second table. If __sub__ () is already defined in the parent class and you define it in the subclass, then the object of the child class will refer to the definition of the subclass, not the definition of the parent class. The same is true for any other attribute.
(A list of special methods is also given at the end of the tutorial)
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 only. This allows you to increase the number of non-existent operations on the object for your own purposes.
Next
The big part of Python's power is that it provides a lot of already written, ready-to-use objects that we've seen built-in such as list tuple and so on. They're easy to use. In Python's standard library, there are plenty of objects that will gladly be used for operating system interactions , Internet development , multithreading , and text processing . On top of all this, there are many external libraries that define richer objects such as NumPy, Tkinter, Django, etc. for scientific computing, GUI development, and web development libraries.
Python Basic Summary