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

How to define a class in python

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to define the class in python, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

The definition of the class # class is the keyword that defines the class, and ClassName is the name of the class class ClassName: # write other content here passclass ClassName (object): # write other content pass here

This is the simplest class definition, which generally inherits the object class after python3, but it doesn't have much of a problem without inheritance.

Class object

Class object is also called instance

# this is to create a class object (instance), and an is an instance and an object a = ClassName () variable

Inside the class are: class variables, local variables, instance variables

Class variable

Class variables: all instantiated objects of all classes share class variables at the same time, that is, class variables exist as common resources in all instantiated objects.

Class ClassName (object): # define a class variable name = "Tom"

There are two ways to call class methods, either directly using the class name or using the class instantiation (object) call. The value of the class variable can be modified by the class name, and when modified, all instantiated objects are affected.

# call print (ClassName.name) # directly through the class name to create an instance (object) of the class a = ClassName () b = ClassName () / / call print (a.name) print (b.name) ClassName.name = "Tim" print (a.name) print (b.name) through the instance of the class

The running result is:

Tom

Tom

Tom

Tim

Tim

However, if the value of the class variable of the instance is modified, the value of the variable is modified through the class name, which will not affect the class variable of the instance.

A = ClassName () b = ClassName () a.name = "asf" ClassName.name = "Pig" print (a.name) print (b.name) ClassName.name = "aaa" print (a.name) print (b.name)

Running result:

Asf

Pig

Asf

Aaa

From the running results, we can see that the value of the name variable of Class Name.name = "Pig" object a does not change after the object a changes the name value.

Local variable

Local variables: local variables are only defined within class methods and can only be used within methods

Class ClassName (object): # this is a method def function (self): # this is a local variable name = "Tom"

Name is defined within a class method, so it is a local variable and cannot be called externally, so it can only be used within a method.

Instance variable

Instance variable: with self. A variable defined by its name, called an instance variable (property)

Class ClassName (object): # this is an instance variable def function1 (self): self.name = "Tom" def function2 (self): print (self.name)

Acts only on objects that call methods, and can be called within other methods.

ClassName = ClassName () className.function ()

Running result:

Tom

If you define an instance variable in the _ _ init__ () method, it can be accessed only through the object name.

Class ClassName (object): def _ _ init__ (): self.name = "Tom" className = ClassName () print (className.name)

Running result:

Tom

Private variable

_ _ name this is a private variable. Add two _ in front of the variable. Self.__name is also a private variable, and private variables cannot be accessed directly outside the class.

Class ClassName (object): # defines a private class variable _ _ name = "Tom" ClassName.__name

Running result:

AttributeError Traceback (most recent call last)

In

2 _ _ name = "Tom"

three

-> 4 ClassName.__name

AttributeError: type object 'ClassName' has no attribute' _ _ name'

Methods of the class

Use the def keyword in a class to define a method, also known as a function. Different from the normal method, you must need a parameter of self, where self represents the class itself.

Class ClassName (object): def function (self,a): pass

Def is the keyword that defines the function, and function is the name of the function

Special method

1. The parameters that are used to initialize classes are generally called constructors.

This method is used by most of our classes.

Class ClassName (object): def _ _ init__ (self): self.name = "aaa"

_ _ init__ is automatically called when the class is instantiated

2. Calling callcalls _ () when the object is treated as a function.

Class ClassName (object): def _ init__ (self): self.name = "aaa" def _ call__ (self): print ("aaa") c = ClassName () / / Class instantiation, _ _ init__ () method c () / _ _ call__ () method is executed

3. The _ _ get__ () class is called automatically when it is an attribute of another class

Class ClassName: def _ _ init__ (self): pass def _ get__ (self,instance, owner): print ("_ _ get__ called") class ClassName2: a = ClassName ()

Run result: _ _ get__ called

4. _ _ new__ () is a static method responsible for creating an instance of the class, and this method takes precedence over the _ _ init__ () initialization method to be called.

. . . There are many other class-specific methods.

Inherit

Inheritance is to get the property of the parents in life, and to get all the variables and methods of the parent class in the programming language. Derive a new class from a class by inheritance

Single inheritance

In short, it inherits a class, the inherited class is the base class (parent class), and the inheritor is the subclass.

# parent class class A: pass# subclass, inheriting class A class B (A): pass

The above is a simple inheritance, and the subclass writes the name of the class to inherit in parentheses after the class name.

# parent class class A: name = "Tom" def getName (self): print ("..") # subclass, inheriting class A class B (A): passb = B () print (b.name) b.getName ()

Running result:

Tom

..

It is found that class B does not write any variables and methods, but calls the variables (properties) and methods of class A.

Of course, if you don't want a variable or method of the parent class, you can override that variable or method

# parent class class A: name = "Tom" def getName (self): print ("..") # subclass, inheriting class A class B (A): def getName (self): print ("bbb") b = B () print (b.name) b.getName ()

Running result:

Tom

Bbb

Multiple inheritance

Multi-inheritance, in terms of name, inherits multiple classes

Class A: passclass B: passclass C (A, B): pass

The above C class inherits both An and B classes.

Thank you for reading this article carefully. I hope the article "how to define classes in python" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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