Detailed explanation of python class instance analysis
This article mainly introduces the detailed explanation and examples of python related information. Friends in need can refer to
detailed explanation of python class
Class
1. A class is a data structure that can be used to create instances. (Generally, a class encapsulates data and methods that can be used for that data)
2. Python classes are callable objects , that is, class objects
3. Classes are usually defined at the top level of a module so that class instances can be created anywhere in the source code file where the class is defined.
4. Instance initialization
instance = ClassName(args....)
Classes can use two special methods, init and del, when instantiating them.
class ClassName(base): 'class documentation string' #类文档字符串 class suite #类体
base: A superclass is a collection of one or more parent classes used for inheritance
-
The class body can include: declaration statements, class member definitions, data Attributes, methods
If the class does not have an inheritance relationship, the parentheses will not be provided. base
class FirstClass(): spam = 30 #类数据属性 def display(self): #类方法 print self.spam x = FirstClass() #创建类实例 x.display() #方法调用 >>> 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
Within the class statement, any assignment statement will create class attributes
Each instance object will inherit the attributes of the class and obtain its own namespace
Python class methods and calls
Attributes contained in the instance (object)
Callable attributes: methods
Data Attribute
In OOP, instances are like records with "data", and classes handle these records" Program”
Calling a method through an instance is equivalent to calling a method of the class to which it belongs to process the current instance. For example, in the previous code example, x.display() will be automatically converted to FirstClass.display(x), that is, the method of the class is called to process the instance x
. Therefore, each method in the class There must be a self parameter, which implies the meaning of the current instance
Assigning the self attribute within the method will generate its own attributes for each instance
-
Python stipulates that methods are not allowed to be called without instances. This is the concept of 'binding'
The assignment statement in the class statement will create class attributes, such as The spam of the previous example
Assigning a value to the special parameter self passed to the method in the class method will create an instance attribute
Python Constructor
When creating an instance, Python will automatically call the init method in the class to provide attributes for the instance invisibly
The init method is called a constructor
If there is no init method defined in the class, the instance is initially created as a simple namespace.
The first parameter of init must be self, self Variable is used to reference the instance to which the method is bound in the class instance method. Because an instance of a method is always passed as the first argument in any method call, self was chosen to represent the instance. You must put self in the method declaration, but you can use the method without an instance (self). If you don't use self in your method, then consider creating a regular function unless you have a specific reason. After all, your method code is not using an instance and has no functionality associated with the class, which makes it look more like a regular function. In other object-oriented languages, self might be called this.
init cannot return any object
##Destructor: del
The constructor is necessary, but the destructor can often be ignored (the Python interpreter will recycle it by itself)
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 Shaw del i1 >>> Shaw id dead.
类的特殊属性
使用dir()或dict,查看类或实例的属性
doc:获取文档字符串
base:获取所有父类
module:类所在的模块
name:实例所属类的名字
Python类方法中可用的变量
继承
继承描述了基类的属性如何‘遗传'给派生类
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
The above is the detailed content of Detailed explanation of python class instance analysis. 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











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
