First of all, I'm spicy chicken, and then this question is really interesting
First of all, a class is a collection, an abstract collection of data and operation descriptions
You can use a class as a container first
class Cycle: def __init__(self,r): self.pi=3.14 self.r=r a=Cycle(4) b=Cycle(7)
You see, we have defined a Cycle class. Now we just use it as a data set. We use the feature of data isolation between instances to ensure that the specific instance data does not pollute each other. Now you want to ask, why do we use data sets to put data
Well, let's see what we would have done without classes. Suppose we were going to calculate the area of a circle now
def square(r,pi): return pi * (r**2) PI=3.14 a_r=4 a_square=square(a_r,PI) b_r=7 b_square=square(b_r,PI)
It seems that there is no problem. OK, now the problem is coming. If you need to calculate the area of many circles now, do you find that the way of isolating data with variable life is becoming more and more dirty. And do you find a lot of redundant code
All right, let's change that
class Cycle: def __init__(self,r): self.pi=3.14 self.r=r def square(value): if not isinstance(value,Cycle): raise ValueError("value muse be Cycle instace") value.square=value.pi * (value.r**2) a=Cycle(4) b=Cycle(7) square(a) square(b)
Well, do you think it's a little clearer now.
PS: no one answers the questions? Need Python learning materials? You can click the link below to get it by yourself
Well, now we can change it
class Cycle: def __init__(self,r): self.pi=3.14 self.r=r def square(self,value): return self.pi * (self.r**2)
OK, now you may be confused. Why do we put the square function in the class?
OK, now I want to calculate the area of all kinds of two-dimensional geometric figures, such as rectangle, prototype and trapezoid. How to write this???
You think about what we said before, using classes as data containers, and you think about writing the following code
class Rectangle: def __init__(self,length,height): self.length=length self.height=height class Cycle: def __init__(self,r): self.pi=3.14 self.r=r def rec_square(value): if not isinstance(value,Rectangle): raise ValueError("value muse be Rectangle instace") value.square=value.length * value.height def cycle_square(value): if not isinstance(value,Cycle): raise ValueError("value muse be Cycle instace") value.square=value.pi * (value.r**2)
If you think about it, does it feel like if there are more and more computing demands, the code will get dirty?
If we put the function in the class and use the inherited feature, we can write such code
class Geometry: def get_square(self): raise NotImplementedError class Rectangle(Geometry): def __init__(self,length,height): self.length=length self.height=height def get_square(self): return self.length*self.height class Cycle(Geometry): def __init__(self,r): self.pi=3.14 self.r=r def get_square(self): return self.pi * (self.r**2) def square(value): if not isinstance(value,Geometry): raise ValueError("value muse be Geometry instace") value.square=value.get_square()
You see, now we only need to expose a unified interface to users. Users (users themselves) don't need to care about how to choose the right function. They just need to call the unified square function to get the specific area. Is it a lot easier??
So, class, it is a kind of encapsulation of data and operation. The significance of this encapsulation is that we can better optimize the code structure.
Well, for another example, we can use classes to control access rights
class People: def __init__(self,website): self.__favorite_website="1024.com" def bad_or_not(self): return self.__favorite_website=="1024.com"
You see, we use the private variable to ensure that some sensitive data cannot be directly accessed by external users (in fact, private is not strict in Python, but it can be accessed after hook)
Well, to give you an example
class People: def __init__(self,website): self.__favorite_website="1024.com" def bad_or_not(self): return self.__favorite_website=="1024.com" @property def favorite_website(self): return self.__favorite_website @favorite_website.setter def favorite_website(self,value): if value=="1024.com": raise ValueError("You can't go to durian, man. You're undernourished") self.__favorite_website=value
You see, it's very convenient for us to filter data values when setting them.
So much back to your question
First of all, there is nothing wrong with what Jun A said, but I want to know that the efficiency of using only function hammer and screwdriver to complete A project is lower than using Class factory + function hammer to complete A project?
Why? When will God consider using Class to improve the "execution efficiency" and "readability" of code. Returning to the actual situation, I often call the same function / method to output a certain result.
I still can't figure out why it's more convenient to call Class? PS: I'm a rookie. I've written thousands of lines of code, but I still don't know what kind of case I need to use Class. I tried to force Class output into my code
But it's not flexible, and there's obviously a lot of code to write and it's not easy to understand. Ask the great God for example, crush my ignorance. C Jun said that large-scale projects will be exhausted if they do not use Class to call objects. Where is this "exhausted" reflected?
First of all, can I implement these codes written in my whole answer without the object-oriented system?
But is it clean? I don't think it's clean.
After the project specifications go up, if we develop in the traditional way, we must check multiple times to make sure that we don't call the wrong things by hand. The idea of OOP, in fact, is to reduce the possibility of our mistakes to some extent by using the appropriate code structure and encapsulation.
At the same time, the current development is not a single fight, the code you write may be used and maintained by others. Let's assume that people who use and maintain your code are idiots. We need to properly encapsulate the code and optimize the structure so that users can make as few mistakes as possible
So finally, whether it's the variable naming rules of different languages or the OOP paradigm. Its essence is a relatively superior compromise between freedom and readability maintainability. The fundamental purpose of this compromise is to reduce the cost of team development and maintenance and optimize the development experience through standardized operation and encapsulation.
In addition, there is an old-fashioned problem of over encapsulation in the development of this set. My personal point of view is that before you know what is over encapsulation, you don't need to consider this issue. According to the structure in textbooks and open source code, you need to constantly encapsulate and optimize your code.
Object oriented is easier to use. The disadvantage is that it is difficult to change a developer, maintain it, understand the thinking of the people in front of it, and find a wrong place