Object: all values used in a program are objects. an object consists of internal attributes and methods. these methods perform various data-related operations, we have used the method before to operate the list type data: Object: all values used in the program are objects, objects consist of internal attributes and methods. these methods perform various data-related operations. we have used the following methods when operating the list data:
data = [37,34]; data.insert(1,25); data.append(23) print(data);
Output result:
37,25, 34,23
The dir () function can list available methods on objects as tools for interactive experience.
dir(data)
Output result:
['_ Add _', '_ class _', '_ ins __',...... 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'delete', 'reverse ', 'sort ']
When you use the dir () function to view object methods, you will find that there are many special methods for starting and ending the following dashes. these methods are used to implement various calculation methods;
For example:
data = data +[3,4,50]; print(data);
In python, the + operator actually performs the following operations:
data.__add__([3,4,5])
Note: whether two objects can be added depends on whether the corresponding object has the _ add _ () method, if this object cannot be added in mathematics, we can use addition to express its defined operations. Therefore, we can draw a conclusion: in python, magic methods like _ add _ generally define their different behavior. the behavior corresponding to _ add _ is a + operation or connection.
In object-oriented programming, class objName class: defines the object class objName: class name
Class Stack (object): def _ init _ (self): #__ init _ constructor. the initialization method self called when an instance is created. 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 the Stack class, the syntax class Stack (object) declares the Stack as an object. parentheses are the methods specified by python for inheritance. Therefore, the Stack class inherits from the object. the object is the root type of all python types.
The def syntax is used in the class to define the first parameter self in each method of the method always points to the object itself.
Self variable: all operations involving objects must reference the self variable.
How to Use Stack?
s = Stack(); s.push("Dave"); s.push([3,4,5,6]); s.push(32); x = s.pop();
Output result
32
y = s.pop();
Output result:
[3, 4, 5, 6]
Append () method: This method is introduced in the list section of python. it is used 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 this element.
Len () function: returns the number of elements in the list.
The Stack class actually creates a brand new object for implementing the Stack, but the Stack is almost identical to the built-in list object. Therefore, we can directly inherit the list and add additional methods.
class Stack(list): def push(self,object): self.append(object);
When the list object is inherited, the method Stack object of the list object can be directly used.
s = Stack(); s.push(66); s.push(65); s.push(64); s.push(63);
Output result:
[66, 65, 64, 63]
S. reverse (); # this method is used to reverse the order of elements in the list.
Output result:
[63, 64, 65, 66]
How do I create static methods in a class?
Generally, all methods defined in a class only use instances of the class (created objects), but static methods can also be defined (called like functions)
class MyObject(object): @staticmethod def myCheck(): return 'ok'; result = MyObject.myCheck(); print(result);
Output result:
OK
@ Staticmethod is a decorator, which is actually a function. when we execute the staticmethod function of the decorator before executing the myCheck function, we first define the myCheck method as a static method.
Summary:
1. dir () method: all methods of the object can be viewed.
2. magic method: a method that ends with a double underline. this method defines different behaviors, for example, __add _ (). The + operation is defined.
3. define class methods in python and inherit objects such as class MyObject)
4, @ staticmethod: decorator usage: defines a method as a static method
The above is the content of the object in python. For more information, see PHP Chinese network (www.php1.cn )!