Object: All values used in the program are objects, and the objects are composed of internal properties and methods that perform various operations related to the data, and we have used the method before manipulating the list type data:
data = [37,34]; Data.insert (1,25); Data.append (+) print (data);
Output Result:
37,25,34,23
The Dir () function can list the methods available on an object as a tool for an interactive experience
Dir (data)
Output Result:
[' __add__ ', ' __class__ ', ' __contains__ ', ... ' append ', ' clear ', ' copy ', ' Count ', ' extend ', ' index ', ' Insert ', ' Pop ', ' R ' Emove ', ' reverse ', ' sort ']
When you use the Dir () function to view the methods of an object, you will find a number of special methods for starting and ending the underscore these methods are used to implement various operations;
For example:
data = Data +[3,4,50]; print (data);
The + operator in Python actually performs the following actions:
DATA.__ADD__ ([3,4,5])
Note: If two objects can be added, the first thing to do is to see if the corresponding object exists the __add__ () method, and if so, we can use the addition to express its defined operations, even if the object is mathematically unavailable, so we can conclude that Python is similar to __add__ Such a magic method generally defines its different behavior __add__ the corresponding behavior is the + operation or the connection
In object-oriented programming, class ObjName classes: Defining ObjName: Class names for objects
Class Stack (object): def __init__ (self): #__init__ constructor, the initialization method called when an instance is created self.stack = []; def push (Self,object): self.stack.append (object); def pop (self): return Self.stack.pop (); def length (self): return len (self.stack);
When defining a stack class, the syntax class stack (object) declares the stack as an object parenthesis, which is how Python specifies inheritance, so the stack class inherits from the root type of all Python types
Class uses DEF syntax to define the first parameter in each method of the method, self always points to the object itself
Self variable: All operations involving an object must refer to the self variable
How do I use the stack?
s = Stack (); S.push ("Dave"); S.push ([3,4,5,6]); S.push (+); x = S.pop ();
Output results
32
y = S.pop ();
Output Result:
[3, 4, 5, 6]
Append () Method: This method is described in the list section in Python to append an element to the end of the list
Pop () method: Used to delete an element from the end of the list and return the element
Len () function: Returns the number of elements in a list
The Stack class actually creates a completely new object for implementing the stack, but the stack is almost identical to the built-in list object, so we can simply inherit the list and add an extra method.
Class Stack (list): def push (self,object): self.append (object);
The method stack object of a list object can be used directly when the list object is inherited
s = Stack (); S.push (a); S.push (+); S.push (+); S.push (63);
Output Result:
[66, 65, 64, 63]
S.reverse (); #此方法 used to reverse the order of the elements in the list
Output Result:
[63, 64, 65, 66]
How do I create a static method in a class?
Typically all methods defined in a class use only instances of the class (objects created) but you can also define static methods (which can be called as functions)
Class MyObject (object): @staticmethod def mycheck (): return ' OK '; result = Myobject.mycheck (); print (result);
Output Result:
Ok
@staticmethod is an adorner (decorator) is actually a function when we execute the MyCheck function before the Staticmethod function of the adorner executes the MyCheck method definition for the static method
Summarize:
1,dir () Method: You can view all the methods of an object
2, Magic Method: The method of ending with double underscore the class method defines different behaviors such as: the __add__ () method defines the + operation
Methods for defining classes in 3,python and how to inherit objects such as: Class MyObject (Object)
4, @staticmethod: Use of adorners: Defining a method as a static method
The above is the content of the object in Python, please pay attention to topic.alibabacloud.com (www.php.cn) for more relevant content.