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 are the common nouns in python object-oriented programming

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what are the common nouns of python object-oriented programming". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the common nouns of python object-oriented programming?"

Definition of object-oriented programming

The core of object-oriented programming is object, that is, a complex thing that can have attributes and actions. Object-oriented programming gives priority to objects and results that need to be achieved, rather than focusing on processes.

The advantage of object-oriented programming is that the program is highly maintainable and scalable. The use of object-oriented programming can greatly improve the team development efficiency, and it is easier and convenient for object-oriented programming to cope with demand changes in the scenario where requirements change rapidly.

The disadvantage of object-oriented programming is that it is not controllable and can not accurately predict the flow and results of the problem.

Common nouns in object-oriented programming

Class: a class of things (such as animals, plants, etc.) with some similar characteristics. Class is a template and an abstract concept.

Object (instance): a concrete thing (such as that stray cat, this pet dog), the object is a concrete thing, is the embodiment of abstract concepts.

Instantiation: the process of creating concrete objects based on a class.

Definition of the class name (parent class):''description document' 'class body

The (parent class) can be defaulted, and the default parent class is object, which can be understood as the ancestor of the class.

The contents at the beginning and end of the third quotation mark on the next line of the class statement are the documentation for the class.

The next line of the description document is the official content of the class, that is, the class body.

There are two kinds of main contents in the class body, one is attribute, the other is function.

For example:

Class Animal:kind = 'all animals'def eat (self): print (' The animal is eatingship') print (Animal.kind) Animal.eat (Animal) out:all animalsThe animal is eating!

In the above case, kind is an attribute and eat is a function.

Note that eat is special because it is a function when called with the class name .eat and a method when called with object .eat.

Instantiation

The class name with parentheses is instantiated, which automatically triggers the operation of the _ _ init__ function, which customizes its own characteristics for each instance based on the parameters.

White_cat = Animal () print (white_cat.kind) white_cat.eat () out:all animalsThe animal is eating!

Note: instantiation will find the _ _ init__ function of this class. If the class does not have this function, it will find the _ _ init__ function of its parent class. If the parent class does not have this function, it will continue to find the grandfather class _ _ init__ function until it finds the empty _ _ init__ function in the object class.

The difference between function and method

When learning python, there are often people who don't know the difference between functions and methods. Here through the case to find out the difference between them.

From types import FunctionType, MethodTypeclass Animal:kind = 'all animals'def eat (self): print (' The animal is eatingship') white_cat = Animal () print (isinstance (Animal.eat, FunctionType)) # determine whether Animal.eat is a function print (isinstance (white_cat.eat, FunctionType)) # determine whether white_cat.eat is a function print (isinstance (Animal.eat, MethodType)) # determine whether Animal.eat is a method print (isinstance (white_cat.eat) MethodType)) # determine whether white_cat.eat is a method out:TrueFalseFalseTrue

Through the above cases, a conclusion is drawn:

Class. When the variable name () is called, the variable name is a function.

When the variable name () is called, the variable name is a function.

An example. When the variable name () is called, it is a method.

At this point, I believe you have a deeper understanding of "what are the common nouns of python object-oriented programming?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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

Internet Technology

Wechat

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

12
Report