Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic usage of class and objects in Python

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what is the basic usage of class and objects in Python. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

This paper mainly introduces the basic usage of class and object such as Python object-oriented, and analyzes in detail the basic concepts, principles, usage and matters needing attention of class class and object in Python object-oriented programming, which can be referred by friends who need it.

Class: defines the abstract characteristics of a thing, usually. A class defines the properties of a thing and what it can do as an object (object): it is an instance of a class.

1. Basic point

< code class="prism language-bash">

Class MyClass (object):

Message = "hello,world"

Def show (self):

Print (self.message)

< /code>

The class name MyClass has one member variable: message, and assigns the initial value.

The member function show (self) is defined in the class. Note that the member function in the class must take the parameter self.

The parameter self is a reference to the object itself, and the information about the object can be obtained by referring to the self parameter in the member function.

Output result:

< code class="prism language-bash">

Inst = Myclass () # instantiate an object of MyClass

Inst.show # calls member functions without passing in the self parameter

Hello,world

< /code>

Note: by adding parentheses after the class name, you can directly instantiate the class to obtain the object variable, and use the object variable to access the member functions and member variables of the class.

two。 Constructor function

Constructor is a special class member method, which is mainly used to create object initialization. The class constructor in python is named with _ _ init__:

< code class="prism language-bash">

Class MyClass (object):

Message = 'Hello, Developer.'

Def show (self):

Print self.message

Def _ init__ (self):

Print "Constructor is called"

Inst = MyClass ()

Inst.show ()

>

< /code>

Print the results:

< code class="prism language-bash">

> Constructor is called

Hello, Developer.

< /code>

Note: constructors cannot have return values, and multiple constructors cannot be defined in python, but objects can be constructed in many ways by providing default values for named parameters.

3. Destructor function

Are constructed reverse functions that are called when objects are destroyed or released.

The method in python that defines a destructor for a class defines a function called _ _ del__ in the class definition that has no return values and parameters.

< code class="prism language-bash">

Class MyClass (object):

Message = 'Hello, Developer.'

Def show (self):

Print self.message

Def _ _ init__ (self, name = "unset", color = "black"):

Print "Constructor is called with params:", name, "", color

Def _ del__ (self):

Print "Destructor is called!"

Inst = MyClass ()

Inst.show ()

Inst2 = MyClass ("David")

Inst2.show ()

Del inst, inst2

Inst3 = MyClass ("Lisa", "Yellow")

Inst3.show ()

Del inst3

>

< /code>

4. Instance member variable

The variables referenced by self are defined in the constructor, so such member variables are called instance member variables in python.

< code class="prism language-bash">

Def _ _ init__ (self, name = "unset", color = "black"):

Print "Constructor is called with params:", name, "", color

Self.name = name

Self.color = color

< /code>

5. Static and class functions:

Python supports two functions that access members based on class names: static functions and class functions.

The difference is that the class function has an invisible parameter cls that can be used to obtain class information. A static function does not have that function.

Static functions are defined with decorator: @ staticmethod

Class functions are defined using the decorator: @ classmethod

< code class="prism language-bash">

Class MyClass (object):

Message = 'Hello, Developer.'

Def show (self):

Print (self.message)

Print ("Here is s in s!" (self.name, self.color))

@ staticmethod

Def printMessage ():

Print ("printMessage is called")

Print (MyClass.message)

@ classmethod

Def createObj (cls, name, color):

Print ("Object will be created:% s (% s,% s)"% (cls.__name__, name, color))

Return cls (name, color)

Def _ _ init__ (self, name = "unset", color = "black"):

Print ("Constructor is called with params:", name, ", color)

Self.name = name

Self.color = color

Def _ del__ (self):

Print ("Destructor is called for s!" self.name)

MyClass.printMessage ()

Inst = MyClass.createObj ("Toby", "Red")

Print (inst.message)

Del inst

< /code>

6. Private member

Python uses the method of specifying the variable name format to define private members, that is, all members named with a double underscore "_ _" are private members.

< code class="prism language-bash">

Class MyClass (object):

Def _ _ init__ (self, name = "unset", color = "black"):

Print "Constructor is called with params:", name, "", color

Self.__name = name

Self.__color = color

Def _ del__ (self):

Print "Destructor is called for s!" self.__name

Inst = MyClass ("Jojo", "White")

Del inst

< /code>

The above is what are the basic uses of class and objects in Python. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report