In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the class of Python". In the daily operation, I believe that many people have doubts about what the class of Python is. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the question of "what is the class of Python?" Next, please follow the editor to study!
All high-level languages have face-to-face objects, and Python is no exception. Compared with traditional languages such as cantileveraged java, face-to-face objects in Python remove a lot of complexity and are easier to understand and use.
Generally speaking, the object includes three characteristics: data encapsulation, inheritance and polymorphism.
1. Classes and instances of classes
A class is actually a data structure that we can use to define objects. A class contains attributes and behavior properties. Classes are real-world abstract entities that appear in programming form.
The class declaration in python is named with the class keyword.
The python class is divided into two types: the classical class (the old class) and the new class. New classes are mainly introduced after Python2.2, and currently mainly use new classes (new classes are the default in Python 3.x).
Perhaps students will ask why there are new classes (the main differences are the built-in properties of the class, the search order of multiple inheritance, and parent class initialization).
The object instance declaration of the class is simple, and the method that implements the instantiation is to call a class as if it were a function.
two。 Initialization of class
The initialization function of the class is completed with _ _ init__. Some students may have the basic knowledge of centering _ initialization java and think it is very similar to the constructor.
Indeed, note that _ _ init__ () is not the constructor of the class, but is just used for initialization.
The _ _ init__ () method with this special name is used to create an instance of the class. Python calls this special method and initializes a new instance with the passed parameter (except for self, which you don't have to pass explicitly, which Python provides automatically).
Today's article only reveals the surface of the class, and when we get familiar with it and reveal the nature of the class, I'll tell you why it's not a constructor, it's just an initialization function.
1)。 When the Dog class declares an instance of a dog_obj object, Python automatically checks that the _ _ init__ () method is implemented and does some initialization of the instance object.
2)。 Some careful friends will ask why this _ _ init__ () function is so strange that it has to start with a double underscore. Wow, you can rush to answer, which has a special meaning in Python. Some naming conventions are used for access control.
3)。 Another student will ask what self is. This parameter is unique to Python. When _ _ init__ () is called, the instance object is passed in as the first parameter, which is the same as the this in java. In other words, self is actually the object address of the class.
3. Deletion of class
Since the class is initialized, it must be deleted, and there will be input and exit. A _ _ del__ () is used in the python to clean up the class's objects.
When the object is unused, it will be cleaned up. Some students will ask ~ ~ how do I know when the object is not used? smart Python uses the method of reference counting to track it.
Add 1 at a time. When the reference count is 0, python has a garbage collection mechanism built in. Once the reference count is 0, the clean object will call the _ _ del__ () method in the class. Is it very smart? in fact, java also has such a built-in function.
4. Properties of the class
Is defined in the class.
Class is defined in the initialization function.
Let's start with the definition in the class. There is a Student class with name and age attributes in it.
Class Student (object):
Name='wang'
Age=10
Name,age is a property of the Student class, so all object instances of Student share name,age
In other words, the name,age in the two objects of your S1 and S2 is the same. If you don't believe it, look:
Let's take a look at the properties of the object, which are all declared in the _ _ init__ () function, that is, self.name and self.age are the properties of the object.
5. Methods in a class
Which method is in the class: the instance method; the method of the class; the static function of the class.
At this point, the study of "what is the class of Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.