In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about what are the object-oriented knowledge points of Python, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Class: used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to each object in the collection. Object is an instance of a class.
Class variables: class variables are common to the entire instantiated object. Class variables are defined in the class and outside the function body. Class variables are not usually used as instance variables.
Data members: class variables or instance variables are used to process data related to a class and its instance objects.
Method rewriting: if the method inherited from the parent class does not meet the needs of the subclass, it can be overwritten. This process is called method overwriting, also known as method rewriting.
Instance variable: a variable defined in a method that acts only on the class of the current instance.
Inheritance: a derived class (derived class) inherits the fields and methods of the base class (base class). Inheritance also allows an object of a derived class to be treated as a base class object.
Create a class
The a variable is a class variable whose value will be shared among all instances of the class. You can use P access in inner or outer classes.
* the _ _ init__ () method is a special method called the constructor or initialization method of a class, which is called when an instance of the class is created
Self represents an instance of a class, and self is necessary when defining the methods of a class, although you do not have to pass in the corresponding parameters when calling.
Self represents an instance of a class, not a class
There is only one special difference between methods of a class and ordinary functions-they must have an extra * * parameter name, which by convention is self.
Enter:
Output:
It is clear from the execution results that self represents an instance of the class and represents the address of the current object, while self.class points to the class.
Self is not a python keyword, and we can change it to runoob:
Create an instance object
Access properties: you can use dots (.) To access the properties of the object. Access the class variable using the name of the following class:
Python built-in class properties
_ _ dict__: the attribute of the class (contains a dictionary consisting of the data attributes of the class)
_ _ doc__: the document string of the class
_ _ name__: class name
The module in which the _ _ module__: class definition resides (the full name of the class is'_ _ main__.className',. If the class is in an import module mymod, then className.__module__ equals mymod)
_ _ bases__: all parent class constituent elements of a class (contains a tuple of all parent classes)
An example of calling Python built-in class attributes is as follows:
Python object destruction (garbage collection)
Python uses the simple technique of reference counting to track and collect garbage.
The number of references to all objects in use is recorded within the Python.
An internal tracking variable, called a reference counter.
When an object is created, a reference count is created, and when the object is no longer needed, that is, when the object's reference count becomes 0, it is garbage collected. However, recycling is not "immediate", and the interpreter reclaims the memory space occupied by junk objects at the appropriate time.
The garbage collection mechanism is not only for objects with a reference count of 0, but also for circular references. A circular reference means that two objects refer to each other, but no other variable references them. In this case, using reference counting alone is not enough. Python's garbage collector is actually a reference counter and a circular garbage collector. As a supplement to the reference count, the garbage collector also pays attention to objects that are allocated a large amount (and those that are not destroyed by the reference count). In this case, the interpreter pauses to try to clean up all unreferenced loops.
Inheritance of class
One of the main benefits of object-oriented programming is code reuse, and one of the ways to achieve this reuse is through inheritance mechanisms. Inheritance can be understood as the relationship between types and subtypes between classes.
Note: the inheritance syntax class derives the class name (base class name): the base class name is written in parentheses, and the base class is specified in the tuple when the class is defined.
Some features of inheritance in python:
1: the construction of the base class (the _ _ init__ () method) in inheritance is not called automatically, it needs to be specifically called in person in the construction of its derived class.
2: when calling the method of the base class, you need to prefix the class name of the base class and take the self parameter variable. Unlike calling ordinary functions in a class, you don't need to take the self parameter.
3:Python always looks for the corresponding type of method first, and if it can't find the corresponding method in the derived class, it starts to look in the base class one by one. First look for the called method in this class, and then go to the base class if you can't find it.
If more than one class is listed in the inheritance tuple, it is called multiple inheritance.
Enter:
Output:
You can also inherit multiple classes:
Calling method override
Enter:
Output:
Class attributes and methods
Private properties of the class
_ _ private_attrs: start with two underscores, declaring that the property is private and cannot be used or accessed directly outside the class. Self.__private_attrs when used in methods within a class.
Methods of the class
Inside the class, you can use the def keyword to define a method for the class. Unlike the general function definition, the class method must contain the parameter self and have * parameters.
Private methods of the class
_ _ private_method: start with two underscores, declaring that the method is private and cannot be called outside the class. Call self.__private_methods inside the class
Enter:
Output:
Single underline, double underline, head and tail double underline description:
_ _ foo__: defines special column methods, such as _ _ init__ () and so on.
_ foo: a variable of type protected that begins with a single underscore, that is, a protected type can only be accessed by itself and subclasses, and cannot be used for from module import *
_ _ foo: the double underscore represents a variable of private type (private), which can only be accessed by the class itself.
After reading the above, do you have any further understanding of Python object-oriented knowledge? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.