Python + Selenium: Encapsulate with classes.[SE08]
by - Thursday, January 1, 1970 at 12:00 AM
Before using class features to encapsulate code, let's briefly introduce the concept of classes in Python.
Written before: Remember, the core of object-oriented entry is that you create an object, and you have all its features (including: class members, class methods).

As in the example below, I use the keyword "class" to create a class, the class name is called "MyClass", and finally, on line 14, I create a variable "MyClass_object", and then use the MyClass() instance melted it.
So, what is the core of object-oriented entry?
Yes, I have all the properties and methods of the class MyClass.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 2022-07-31
# Breached.to Bullet Class:
# THE FAST ONLINE VERSION 1.0CB
class MyClass:

    two = "is two"
    three = "is three"
    Four = "is four"


if __name__ == '__main__':
    # Create a variable to instantiate.
    MyClass_object = MyClass()
    # Debug output the value of this class member (attribute) variable
    print(MyClass_object.Four)

As you can see, there are 4 class members in this MyClass, and I can access .

Extended reading:
So, what exactly is a class? In the end how to understand the "class concept"?
I'm not going to talk about the very abstract concept of classes at length, like in many books, repeating those words.
If you understand how computers store variables, then everything will be easier.
We know that when you are programming, each variable has an address in the computer memory to store the variable.
#########################################################################
As for how the computer saves and reads, you don't need to understand for the time being, if you are interested
You can go to search "About Windows platform 8086 register addressing and X86 assembly basis, executable file PE format" The computer uses a specific method to access this variable (base address + offset).
#########################################################################
So, we return to the essence of Python classes. Focus on this sentence:

# Create a variable to instantiate.
    MyClass_object = MyClass()

When the program is executed, MyClass() will be allocated memory space.
So, at this point, I created a variable "MyClass_object" and pointed to MyClass(), then I can access other guys in the memory address of MyClass().
Remark:
In high-level languages, we will learn to manipulate memory, then, if you have the foundation of other languages, you can understand this sentence well, in fact, here is to create a reference. In C language, we use the address operator "&" to operate, then, what is MyClass_object?
"MyClass_object is a reference to MyClass()."
If you understand the last sentence, then you can get started with the Python language. Remember what we said in the previous course, in the Python language, everything is an object! object! object!
The only difference is that there are "deep copies" and "shallow copies" in Python. Here we ignore it for now. If you are interested in understanding "deep copy" and "shallow copy", you can refer to the related documentation.

Let's go back to the topic and continue to encapsulate. I can now access the members and properties of the class through MyClass_object. So, what are the class methods?
Nothing special, remember one sentence: put the function module you wrote into the class, then, this is called "class method"
See example:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 2022-07-31
# Breached.to Bullet Class:
# THE FAST ONLINE VERSION 1.0CB
class MyClass:

    two = "is two"
    three = "is three"
    Four = "is four"

    def Method_Class_Hello(self):
        """
        This is a class method, nothing special, just outputs debug information.
        :return:
        """
        print("Method_Class_Hello is called.")


if __name__ == '__main__':
    # Create a variable to instantiate.
    MyClass_object = MyClass()
    # Debug output the value of this class member (attribute) variable
    print(MyClass_object.Four)
    # called Method_Class_Hello
    MyClass_object.Method_Class_Hello()

Method_Class_Hello is a test function. Many friends will feel strange when they see it here. Why is there an extra parameter "self" in the parameter position?
Let's understand it simply: self refers to the object itself. Who instantiates the class MyClass, at this time, the self points to this object.
Then, we use the variable MyClass_object to instantiate MyClass(), then obviously self points to MyClass_object.
It will be very confusing here, just like the R&P culture in the United States, but we have briefly introduced the concept of "reference" before, so let's think about it in depth here, MyClass() is useful for its own memory space in memory. To save your own member properties, variables, class methods. You suddenly create a MyClass_object and want to have everything I have, so this self is the credentials to make sure you can have MyClass().

Well, this class is very "R&P", please try to understand. If you don't understand it, it doesn't matter, because I will guide you to continue learning Python. Believe me, even if you don't understand classes, you can still play Python.
Reply


 Users viewing this thread: Python + Selenium: Encapsulate with classes.[SE08]: No users currently viewing.