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 Python object oriented

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

Share

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

This article introduces the relevant knowledge of "how to define Python object-oriented". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

object-oriented

Class-class

Object

Class Student (): # define a class name =''# define variable age = 0 def print_file (self): # define a method print ("name:" + self.name) print ('age:'+str (self.age)) # output student = Student () # instantiate an object student.print_file () # call method

[note]: self must be written in each method parenthesis, which is equivalent to the this keyword in java.

But self is not strictly the keyword of python. Self can be any string that matches, but self is officially recommended by python.

Classes and objects: a class, like a template, can produce a variety of objects

Constructor function

Def _ _ init__ (self,name): # Constructor self.name=name self.source= 0 print (name) # call instance variable mode 1 print (self.name) # call instance variable mode 2 print (self.__class__.name) # call class variable mode 1

Some features are similar to those of java.

Class variables and instance variables

Instance variables: the variables called through the instance object and the constructor class code defined in the constructor as above

Print (name) # call instance variable method 1print (self.name) # call instance variable mode 2

Class variables: called directly through the class name, or using self.class in the instance function. The code called by the variable name is like the above code

Print (self.__class__.name) # call class variable mode 1Student.name # call class variable mode 2

Method

Example method

It is a general definition method to associate instance objects.

Def show (self): pass

[note]: the keyword pass is equivalent to a placeholder. You can use it when you haven't figured out the logic yet. Anyway, the code reports an error.

Class method

Use the decorator @ classmethod and the required parameter cls on the method

# Class method @ classmethod def plus_sum (cls): cls.sum + = 1 print (cls.sum) # call class variable

[note]: cls strings are similar to self, except that they can only be used on class methods

Calling a class variable in a class method is different from an instance method.

Self.__calss__.name # instance method invocation class variable cls.name # class method invocation class variable

Static method

Use the decorator @ staticmethod on the method. The difference between the class method and the class method is that you do not need to pass cls. Generally, it is not recommended to use it.

# static method @ staticmethod def plus_sum (): pass

Key points: generally ensuring the security of data can not directly assign values to data, but should be completed in the method, which can be judged in the method, which reflects the object-oriented encapsulation of python.

Private ownership

Proprietary method

Precede the method with a'_ 'double underscore

Def _ _ sum (self): pass

An error will be reported if the private method is called forcibly outside.

Private variable

Precede the variable with a'_ 'double underscore

_ _ name = 'millet'

Unable to make private variable call

Student = Student () # instantiate print (student.__name)

Because of the dynamic mechanism of python, a variable _ _ name is actually redefined here.

You can use _ dict _ to view all the variable information of the current object

Print (student.__dict__) # output details

As you can see from the output, the original variable has become the class name plus the variable name such as: _ Student__name

Object oriented-inheritance

The inheritance feature of python is similar to that of java. I believe that those with java inheritance will understand it better.

Parent class

# parent class 2019-8-28class Human (): sum = 0 def _ init__ (self,name,age): self.name=name self.age=age def show (self): print ("parent method")

Subclass

From M4 import Human # Import parent class M4 file name Human as class name class Student (Human): # inherit parent class def _ _ init__ (self,school,name,age): self.school=school # to call the constructor of the parent class needs to add self mode-Human.__init__ (self,name,age) # mode 2 use the super keyword super (Student,self). _ init__ (name,age) def out (self): # call the parent method super (Student) Self) .show () print ("come on") ss = Student ('social', '444th) 5) print (ss.name) print (ss.age) ss.out ()

Two ways to call the parent class

Mode one

# to call the constructor of the parent class, you need to add the self mode-Human.__init__ (self,name,age)

Mode two

Use the keyword super

# Mode 2 uses the super keyword super (Student,self). _ _ init__ (name,age) "how to define Python object-oriented". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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