Python class details and simple examples, python detailed examples
Python
Class
1. A class is a data structure that can be used to create an instance. (In general, classes encapsulate data and methods that can be used for this data)
2. the Python class is a callable object, that is, a class object.
3. Classes are usually defined at the top layer of the module so that class instances can be created anywhere in the source code file defined by the class.
4. instance Initialization
- Instance = ClassName (args ....)
- Classes can be instantiated using two special methods: _ init _ and _ del.
Class ClassName (base): 'class documentation string' # class document string class suite # class body
- Base: A superclass is a set of one or more parent classes for inheritance.
- The class body can include declaration statements, class member definitions, data attributes, and methods.
- If the class does not have an inheritance relationship, the base in parentheses is not provided.
Class FirstClass (): spam = 30 # class data attribute def display (self): # class method print self. spam x = FirstClass () # create a class instance x. display () # method call >>> 30 dir (FirstClass) >>> ['_ doc _', '_ module _', 'display ', 'Spam']
- The class statement is similar to def, and is executable code. The class will not be created until the class statement is run.
- In a class statement, any value assignment statement creates class attributes.
- Each instance object inherits the attributes of the class and obtains its own namespace.
Python methods and calls
Attributes of an instance (object)
- Callable attributes: Methods
- Data attributes
In OOP, an instance is like a record with "data", and a class is a "program" that processes these records"
- Using an instance call method is equivalent to calling the method of the class to process the current instance. For example, in the previous code example, x. display () is automatically converted to FirstClass. display (x), that is, calling the class method to process instance x
- Therefore, each method in the class must have the self parameter, which implies the meaning of the current instance.
- Assigning values to the self attribute in the method will generate the attributes of each instance.
- Python requires that methods cannot be called without instances. This is the concept of 'binding '.
- The value assignment statement in the class statement creates class attributes, as shown in the spam example above.
- Assign values to the special parameter self passed to the method in the class method to create instance attributes.
Python Constructor
When an instance is created, Python automatically calls the _ init _ method in the class to provide properties for the instance implicitly.
- The _ init _ method is called a constructor.
- If the _ init _ method is not defined in the class, the instance is just a simple namespace at the beginning of creation.
- The first parameter of _ init _ must be self. The self variable is used to reference the instance bound to the method in the class instance method. Because the Method Instance is always passed as the first parameter in any method call, self is selected to represent the instance. You must put self in the method declaration, but you can not use the instance (self) in the method ). If self is not used in your method, consider creating a regular function unless you have a special reason. After all, your method code does not use an instance and is not associated with its function with the class, which makes it look more like a regular function. In other object-oriented languages, self may be called this.
- _ Init _ No objects can be returned.
- Destructor __del __
- Constructor is necessary, while destructor can often be ignored (the Python interpreter recycles the constructor)
class MyClass(): def __init__(self, name): self.name = name print 'My name is ' + self.name def __del__(self): print self.name + ' is dead.'i1 = MyClass('Shaw')>>> My name is Shawdel i1>>> Shaw id dead.
Special attributes of a class
- Use dir () or _ dict __to view the attributes of a class or instance.
- _ Doc __: get the document string
- _ Base __: get all parent classes
- _ Module __: module of the class
- _ Name __: name of the class to which the instance belongs
Available variables in Python Methods
- Instance variable: self. variable name
- Local variable: The variable created inside the method, which can be directly used.
- Static variables: variables defined in the class. Class Name. variable name
- Global variable: used directly
Inheritance
Inheritance describes how to inherit the attributes of the base class from the derived class.
- A subclass can inherit any attributes of its base class, including data attributes and methods.
- A class without a specified base class. By default, it has a base class called object.
- Python allows multiple inheritance (multiple parent classes can be inherited)
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!