current position:Home>Summary of Python object-oriented details

Summary of Python object-oriented details

2022-07-25 13:59:42Apple sister

As we all know ,python It's a very “ silence ” Language , Not only support dynamic types , For object-oriented usage, you can also maximize “ laissez-faire ”, Until the program can't run at all, it will ruthlessly report errors , But it also brings many pits , Without further study , It is likely to cause very hidden logical errors .

Recently, due to the expansion of business needs , Many kinds of logic originally written dead 、 Parameters 、 Threshold needs to support both default mode and parameter transfer function , And support uploading of multiple different environments at the same time 、 download 、 Call mechanism , So we need to correct the original python The code base does a lot of refactoring , Inevitably encountered python Many pits of object orientation . In order to get to the bottom of , The author did a wave of experiments , Some may not be met under normal circumstances , But it is helpful for in-depth understanding python Object oriented mechanism , It is arranged as follows :

One 、 Class variables ( Static variables ) And the usage of instance variables

  1. Usage in the same class :

1) Class variables can be accessed by class name or by instance self visit , Instance variables can only be passed through instances self visit

2) Suppose there are class variables and instance variables with the same name ,self Only instance variables can be accessed , Class variables can only be accessed by class name

  1. Usage in class inheritance

1) Subclasses must explicitly initialize the parent , Can inherit the instance variable of the parent class , And you can only use examples self visit , Do not explicitly initialize class variables that can only inherit the parent class , Or don't write __init__ function , The parent class is used by default __init__ Function initialization

2) Subclasses can inherit the instance variables of the parent class , Only use self visit

2) Subclasses can inherit the class variables of the parent class , It can be used self/super()/ Subclass class name / The parent class name accesses the class variables of the parent class ( Subclass class name is also ok !!), If the parent class has an instance variable with the same name , For subclass self Only instance variables of the parent class can be accessed , use super()/ Subclass class name / The parent class name can only access the class variables of the parent class

  1. Subclasses can override the instance variables of the parent class , Instance only self Access instance variables of subclasses , Cannot access the instance variable of the parent class ( Because the parent class is not instantiated when the child class is instantiated ,super() All you get is the class object of the parent class !!)

4) Subclasses can override class variables of the parent class , It can be used self/ Subclass class name access subclass class variables , You can use the parent class name /super() Access the class variables of the parent class . If the parent class has an instance variable with the same name , For subclass self Only instance variables of the parent class can be accessed ( It's amazing !!)
5) The protection attribute of the parent class (_ start ) Can be accessed by parent instances and subclasses , Private property (__ start ) Can only be accessed inside a class , But because of python The pseudo private nature of , Outside the class, you can use instances ._ Class name __ Variable name access

summary :

  1. use self Access instance variables first , Even inherit the instance variables of the parent class , Only when the subclass and parent cannot be found will the class variable be accessed

  2. To avoid mistakes , Try to use the class name to access class variables , Unless inheritance

Two 、 Class method ( Static methods ) And the usage of instance methods

  1. Usage in the same class :

1) Class methods can be accessed by class name or by instance self visit , Instance methods can only be passed through instances self visit

2) Class methods and instance methods with the same name are not allowed , If there is , The lower one will cover the upper one

  1. Usage in class inheritance

1) Subclasses can not explicitly initialize the parent class , You can inherit the class methods and static methods of the parent class ( Note that it is different from the instance variable , Instance variables must be instantiated to exist , The instance method does not depend on the instantiated object !!)

2) Subclasses can inherit the instance methods of the parent class , Only through self visit

3) Subclasses can inherit the class methods of the parent class , It can be used self/super()/ Subclass class name / The name of the parent class accesses the class method of the parent class

4) Subclasses can override instance methods of the parent class , Examples can be used self Access instance methods of subclasses , It can also be used. super() Access the instance method of the parent class ( Note that it is also different from instance variables !!)

5) Subclasses can override the class methods of the parent class , Examples can be used self/ Subclass class name access subclass class method , You can also use the parent class name /super() Access the class methods of the parent class

6) When a subclass overrides a parent method , The number of parameters must be the same

7) Protection method of parent class (_ start ) Can be accessed by parent instances and subclasses , Private method (__ start ) Can only be accessed inside a class , But because of python The pseudo private nature of , Outside the class, you can use instances ._ Class name __ Method name access

summary : Methods and attributes are used in the same way in most cases , There are two main differences : Instance methods do not depend on instances , And it is not allowed to have the same name as a static method .

Please add more details ~

copyright notice
author[Apple sister],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/206/202207250921469349.html

Random recommended