What is class in python
There are two important concepts in the object-oriented programming process: class (class) and object (object, also known as instance, instance), where a class is a certain batch of The abstraction of objects can be understood as a certain concept; the object is a concrete entity. In this sense, what we call people in daily life are actually human objects, not human beings.
The simple syntax for defining a class in Python is as follows:
class 类名: 执行语句... 零个到多个类变量... 零个到多个方法...
The class name only needs to be a legal identifier, but this only meets the grammatical requirements of Python: if it is readable from the program From a gender perspective, Python's class names must be concatenated by one or more meaningful words. The first letter of each word is capitalized, and the other letters are all lowercase. Do not use any separators between words.
Judging from the above definition, Python's class definition is a bit like a function definition. They all start with a colon (:) as the class body and use the uniformly indented part as the class body. The difference is that function definitions use the def keyword, while class definitions use the class keyword.
Python's class definition consists of a class header (referring to the class keyword and class name part) and a uniformly indented class body. The two most important members in the class body are class variables and methods. If no class variables and methods are defined for the class, then the class is equivalent to an empty class. If the empty class does not require other executable statements, the pass statement can be used as a placeholder. For example, the following class definition is allowed:
class Empty: pass
Generally speaking, empty classes do not have much practical significance.
The definition order of the members in the class has no effect, and the members can call each other.
The two most important members of a Python class are variables and methods. The class variables belong to the class itself and are used to define the state data contained in the class itself: while the instance variables belong to the objects of the class. , used to define the state data contained in the object: methods are used to define the behavior or function implementation of the object of this class.
Python is a dynamic language, so the class variables contained in its classes can be dynamically added or deleted (when the program assigns values to new variables in the class body, it adds class variables), and the program can also add class variables anywhere. Add variables to existing classes; the program can delete class variables of existing classes through the del statement.
Similarly, the instance variables of Python objects can also be dynamically added or deleted (as long as a new instance variable is assigned a value, the instance variable is added), so the program can add instance variables to its own objects anywhere; the program Instance variables of existing objects can be deleted through the del statement.
The methods defined in the class are instance methods by default. The method of defining instance methods is basically the same as the method of defining functions, except that the first parameter of the instance method will be bound to the caller of the method (the class instance), so instance methods should define at least one parameter, which is usually named self.
Note: The first parameter of the instance method does not have to be called self. In fact, it can be called any parameter name. It is just a convention to name the parameter self, which has the best readability.
There is a special method in the instance method: __init__. This method is called the constructor method. The constructor is used to construct an object of this class, and Python returns an object of this class by calling the constructor (no need to use new).
Many methods in Python that start with a double underscore and end with a double underscore have special meanings. These special methods will be introduced in detail later in this tutorial.
The constructor is the fundamental way for a class to create objects, so Python also provides a function: if the developer does not define any constructor for the class, then Python will automatically define a constructor for the class that contains only one self. The default constructor for parameters.
The following program will define a Person class:
class Person : '这是一个学习Python定义的一个Person类' # 下面定义了一个类变量 hair = 'black' def __init__(self, name = 'Charlie', age=8): # 下面为Person对象增加2个实例变量 self.name = name self.age = age # 下面定义了一个say方法 def say(self, content): print(content)
The above Person class code defines a construction method. The construction method only has a special method name: __init__, the first part of the method One parameter is also self, bound to the object initialized by the constructor.
The above is the detailed content of What is class in python. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Concepts and instances of classes and methods 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. Method: Function defined in the class. Class construction method __init__(): The class has a special method (construction method) named init(), which is automatically called when the class is instantiated. Instance variables: In the declaration of a class, attributes are represented by variables. Such variables are called instance variables. An instance variable is a variable modified with self. Instantiation: Create an instance of a class, a specific object of the class. Inheritance: that is, a derived class (derivedclass) inherits the base class (baseclass)

jQuery is a classic JavaScript library that is widely used in web development. It simplifies operations such as handling events, manipulating DOM elements, and performing animations on web pages. When using jQuery, you often encounter situations where you need to replace the class name of an element. This article will introduce some practical methods and specific code examples. 1. Use the removeClass() and addClass() methods jQuery provides the removeClass() method for deletion

Class is a keyword in Python, used to define a class. The method of defining a class: add a space after class and then add the class name; class name rules: capitalize the first letter. If there are multiple words, use camel case naming, such as [class Dog()].

When writing PHP code, using classes is a very common practice. By using classes, we can encapsulate related functions and data in a single unit, making the code clearer, easier to read, and easier to maintain. This article will introduce the usage of PHPClass in detail and provide specific code examples to help readers better understand how to apply classes to optimize code in actual projects. 1. Create and use classes In PHP, you can use the keyword class to define a class and define properties and methods in the class.

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? In Vue development, we often use the v-bind instruction to dynamically bind class and style, but sometimes we may encounter some problems, such as being unable to correctly use v-bind to bind class and style. In this article, I will explain the cause of this problem and provide you with a solution. First, let’s understand the v-bind directive. v-bind is used to bind V

Background Recently, key business codes have been encrypted for the company framework to prevent the engineering code from being easily restored through decompilation tools such as jd-gui. The configuration and use of the related obfuscation scheme are relatively complex and there are many problems for the springboot project, so the class files are encrypted and then passed The custom classloder is decrypted and loaded. This solution is not absolutely safe. It only increases the difficulty of decompilation. It prevents gentlemen but not villains. The overall encryption protection flow chart is shown in the figure below. Maven plug-in encryption uses custom maven plug-in to compile. The class file specified is encrypted, and the encrypted class file is copied to the specified path. Here, it is saved to resource/corecla.

How jquery determines whether an element has a class: 1. Determine whether an element has a certain class through the "hasClass('classname')" method; 2. Determine whether an element has a certain class through the "is('.classname')" method.
![How to solve the '[Vue warn]: v-bind:class/ :class' error](https://img.php.cn/upload/article/000/465/014/169300902772563.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
How to solve the "[Vuewarn]:v-bind:class/:class" error During the development process of using Vue, we often encounter some error prompts. One of the common errors is "[Vuewarn]:v-bind:class" /:class" error. This error message usually appears when we use v-bind:class or :class attribute, indicating that Vue cannot correctly parse the class value we set. Then, if
