Home  >  Article  >  Backend Development  >  Why use class in python

Why use class in python

(*-*)浩
(*-*)浩Original
2019-07-20 13:18:174974browse

Python has been an object-oriented language from the beginning. Because of this, it is easy to create classes and objects in Python. In this chapter we will introduce Python's object-oriented programming in detail.

Why use class in python

If you have not been exposed to object-oriented programming languages ​​​​before, you may need to first understand some basic features of object-oriented languages ​​​​and form an idea in your mind. Basic object-oriented concepts will help you learn object-oriented programming in Python more easily. (Recommended learning: Python video tutorial)

Introduction to object-oriented technology

Class (Class): used to describe the same attributes and methods A collection of objects. 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 are used to process data related to the class and its 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 rewriting.

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

Inheritance: That is, a derived class inherits the fields and methods of the 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, so Dog is also an Animal.

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

Method: Function defined in the class.

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

Compared with other programming languages, Python adds a class mechanism without adding new syntax and semantics as much as possible.

Python class creation

Object-oriented programming is a programming method. The implementation of this programming method needs to be implemented by using "classes" and "objects". Therefore, object-oriented programming is actually the use of "classes" and "objects".

A class is a template. The template can contain multiple functions, and the functions implement some functions.

Objects are instances created based on the template. The instance objects can execute the functions in the class. Function

#创建类
class Foo: #class 是关键字(表示要开始创建类了);Foo是新建的类名

  def bar(self):   #self特殊参数(必填)
      pass
#根据Foo创建对象obj
obj = Foo

For more Python-related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of Why use class 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
Previous article:How to count in pythonNext article:How to count in python