“This is the 14th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Official Python column number 37, stop! Don’t miss this zero-based article!

The class and object are familiar to readers who have been presented and shared the concepts of object-oriented programming.

Let’s take a closer look at the structure of the class.

Other built-in functions/attributes in the class

The previous code shows the ‘__init__’ function, which is a built-in function of the class and does no extra operations if not written by default.

In Python, the class structure also contains the following built-in function properties:

  • __name__ class name
  • __dict__ class namespace dictionary (here will not explain more, in fact, although this explanation is abstract but accurate, the following code can be understood in a second)
  • Documentation of the __doc__ class
  • The __module__ class is documented
  • Documentation of the __bases__ class

Look boring, except for the name of the class, we should look like the name of the class.

Let’s take a look at the code and get a feel for it:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2021/11/15 11:58 PM
# @Author : LeiXueWei
# @csDN /Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : __init__.py.py
# @Project : hello

""" Here is a student class definition """


class student(object) :
    """ Here is a student class definition """

    def __init__(self, name) :
        self.name = name

    def get_name(self) :
        return self.name

    def set_name(self, name) :
        self.name = name

    def study(self) :
        print(f"{self.name}Good good study, day day up!")


s1 = student("Student Committee fan [Xiao Bai]")
print(s1)
print(s1.get_name())
print(s1.study())

print("The name of the class.", student.__name__)
print("Class document :", student.__doc__)
print("Class namespace :", student.__dict__)
print("Base class of class :", student.__bases__)
print("Class in module :", student.__module__)
Copy the code

Here’s how it works:

Ok, we see class namespace printed dict dictionary, placed within the content of the key is a member of the class (function, attribute), it is clear: Python in a dict, describe the structure of the class! (This is the network mentioned in the previous part of the study committee, which attaches single functions, data attributes, to this network, forming the structure of the class).

Several other built-in class functions are easy to understand.

The other place is the ‘__bases__’ function, which tells us that the Student class is created from the ‘object’ class. (Indeed!)

Now that we’re talking about the Base class, let’s talk about class inheritance

An explanation of the value returned by the bases function above, in other words:

All classes except the object class are directly or indirectly derived by default from the Object class. That is, the object class is the root, source class of all classes.

Class A inherits from class B, so we will use the following description to represent them:

Class A is A subclass of class B which is the parent of class A

This is just like a family tree, the first grandparent at the top, and that’s the relationship between a class and an object.

#! /usr/bin/env python # -*- coding: utF-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: # @xueweitag: CodingDemo # @xueweitag: CodingDemo # @xueweitag: __init__.py. Py # @project: hello """ Pass print("programmer's namespace :", "programmer.__dict__ ") __bases__) class Student(object): """ "def __init__(self, name): self.name = name def get_name(self): return self.name def set_name(self, name): self.name = name def study(self): Print (f"{self.name} : print(f"{self.name}) Student(" s1 ") print(s1) print(s1.get_name()) print(s1.study()) print(" Student. __name__) print (" type of document: ", Student. __doc__) print (" class namespace: ", Student __dict__) print (" is the base class for class: ", Student.__bases__) print(" module :", student.__module__) class PrimarySchoolStudent(Student): Pass print("*" * 16) print("PrimarySchoolStudent :", PrimarySchoolStudent.__dict__) print("PrimarySchoolStudent :", PrimarySchoolStudent.__bases__) print("PrimarySchoolStudent ", Primaryschoolstudent.__module__) PrimarySchoolStudent(" primary student ").study()Copy the code

This code is trying to show a lot of information.

The first is the addition of a Developer class (no write inheritance) after the first code.

We can see that it inherits from the object class (so there is no difference between writing and not writing).

Second, we see that the output from PirimarySchoolStudent’s bases function is Student. Yes, the base value inherited from a certain class is that class, Student

Third, inheritance gives subclasses direct access to the parent class’s functions and data attributes.

Ok, take a closer look at the following results:

conclusion

This time I’ve added classes and objects (but Python also supports multiple inheritance, which is a big difference from Java!). More than half are uncommon, but useful when writing frameworks or in-depth analysis, so be sure to familiarize yourself with them.

The committee has been writing Java for more than a decade, but this Python tutorial is very pragmatic, so if you have any questions about basic programming, please see the related article.

For those who like Python, please check out the Python Basics section or the Python Getting Started to Master Section

Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to wechat, like support collection!