Home >Backend Development >Python Tutorial >__init__ usage in Python

__init__ usage in Python

Guanhui
GuanhuiOriginal
2020-06-02 14:08:3111137browse

__init__ usage in Python

__init__ Usage in Python

The object construction method in Python is the __init__ method. The function of this method is to convert the object into To initialize properties and methods, you only need to define them in the class when using them. The first parameter of the method must be the current object, and other parameters can be customized.

Code examples

class Student():
    def __init__(self, score1, score2, score3):
        self.scores = [score1, score2, score3]

When used

stu_1 = Student(80, 90, 85)

Introduction to object-oriented technology

  • Class (Class): Used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to every object in the collection. Objects are instances of classes.

  • Class variables: Class variables are public throughout the instantiated object. Class variables are defined in the class and outside the function body. Class variables are generally not used as instance variables.

  • Data members: class variables or instance variables, used to process data related to classes and their instance objects.

  • Method rewriting: If the method inherited from the parent class cannot meet the needs of the subclass, it can be rewritten. This process is called method override, also known as method rewrite.

  • Local variables: Variables defined in methods only act on the class of the current instance.

  • Instance variables: In the declaration of a class, attributes are represented by variables. Such variables are called instance variables and are declared inside the class declaration but outside other member methods of the class.

  • Inheritance: A derived class inherits the fields and methods of a base class. Inheritance also allows an object of a derived class to be treated as a base class object. For example, there is such a design: an object of type Dog is derived from the Animal class, which simulates the "is-a" relationship (for example, Dog is an Animal).

  • Instantiation: Create an instance of a class, a specific object of the class.

  • Method: Function defined in the class.

  • Object: An instance of a data structure defined through a class. Objects include two data members (class variables and instance variables) and methods.

Recommended tutorial: "Python Tutorial"

The above is the detailed content of __init__ usage in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn