“This is the fifth day of my participation in the First Challenge 2022. For details: First Challenge 2022”

How to Be “object oriented”

You’ve been screaming “object oriented programming” to us miserable programmers every day, but give me a new object. You see, I don’t have objects, how can I object-oriented programming. There are plenty of blog posts on the web about object-oriented programming in Java and C++**, so why am I writing? Because life is short, and I happen to be a Python student…

Today, we’ll take a closer look at object-oriented Programming (OOP) in Python.

“Day by day we’ll grow up, I don’t care if you understand what I’m writing… Time flies away and never looks back…”

Object-oriented programming of programming

1. Why programming?

In architecture, the average architect would not want to add a basement to a 100-story building, because doing so would undoubtedly be too expensive and would probably fail. (I just passed the first-grade architect exam in the first half of the year, and the 3.5 hours of site design and drawing seemed difficult.) Surprisingly, in our software development industry, users don’t think twice when they propose changes like this. Instead, the boss or product manager might say it’s a simple programming problem. (Haha, no harm done to programmers and product managers…)

But software is inherently complex, and as projects iterate, complexity often outpaces human intelligence.

2. Is there a “best” design?

In every kind of engineering practice, design is a disciplined method. Whether it’s functional programming, generic programming, concurrent programming, process-oriented, object-based, object-oriented programming, we create a solution to a problem through design methods that provide access to time requirements.

So, is there a “best” way to design?

There may be no absolute answer to this question, but No “Silver Bullet” shows us that no single technological or managerial advance can safely enable software engineering to achieve a complex system from requirements. So, object orientation is not the last “silver bullet” to solve all problems in software development. Many high-level programming languages today offer a variety of programming design paradigms, and Python is no exception.

3. Why object-oriented programming?

Classes are the main tool of object-oriented programming, and we use them to define new kinds of ways that reflect real objects in the program domain. And what is object-oriented programming?

Object-oriented programming is an approach to implementation in which programs are organized into groups of cooperating objects, each representing an instance of a class that belongs to a hierarchy formed through inheritance relationships.

Take a look at three key points of the concept:

  1. Use objects as the basic unit of a program, not algorithms
  2. Each object is an instance of some class
  3. Classes and classes can be related by inheritance

These three things are called object-oriented programs. Let’s simulate this with a simple example

Next, let’s look at the three core concepts of object orientation:

  1. Inheritance. Colloquially understood as “what a son inherits from his father.” If you create a new class based on a class, the attributes and methods of the parent class will be directly inherited, thus reducing the need to write repetitive code. Those that provide inherited information (property) are called superclasses, or base classes, or superclasses. We call subclasses, or derived classes, or derived classes, when we get inherited information.
  2. Polymorphism. Literally, “multimodal,” the different implementations of interfaces are polymorphic. By polymorphism, a subclass can extend the capabilities of its parent or overwrite the operations of its parent.
  3. Encapsulation. I understand encapsulation as a black box with some functionality that hides any implementation details that can be hidden and then provides a simple programming interface.

Object-oriented programming provides an efficient way to program with minimal code redundancy. Therefore, we can write new programs by customizing existing code rather than making changes in place.

Everything in Python is an object

OOP is not required at all in Python, classes are not required in the beginning stages, and functional structures allow you to write great scripts and do interesting programming. But Python OOP is also a lot of fun, if you want to learn with me.

Java, while also an object-oriented programming language, has less pedigree than Python. For example, int, one of Java’s eight basic data types, needs to be wrapped as an Integer class object when persisted.

Python is more thoroughly object-oriented than Java. Those of you who have studied Python probably know that in Python, you turn everything you can see into objects — numbers, strings, tuples, lists, dictionaries, functions, methods, classes, modules, including your code.

How do you understand this process? Everything in Python can be assigned to a variable or passed as an argument to a function. Let’s look at the code:

a = 3
b = a
print(a)  Print: 3
print(b)  Print: 3

def all_is_object() :
    print("Learing Python OOP")
    
all_is_well = all_is_object
all_is_object() # Print: Learing Python OOP
all_is_well()  # Print: Learing Python OOP

class Person() :
    def __init__(self, name) :
        print("Name:", name)
        
A = Person
xiaoyu = A("Youth")  # Name: Xiaoyu
Copy the code

Three features of Python objects

All Objects in Python have three features: id, type, and value.

  • Identity (ID) : Each object has a unique identity that identifies itself. The identity of any object can be obtained using the built-in function id(), which can simply be thought of as the object’s memory address.
a = 3
b = a
id(a)
id(b)
id(test_list)
Copy the code
  • Type: The type of an object determines which types of values it can hold, which properties and methods it has, which operations it can perform, and which rules it follows. You can use the built-in function type() to see the type of an object.
a = 3
b = a
print(type(a))  # <class 'int'>
print(type(int))  # <class 'type'>

test_list = [1.2.3.4.5]
print(type(test_list))  # <class 'list'>
print(type(list))  # <class 'type'>

test_tuple = (1.2.3.4.5)
print(type(test_tuple))    # <class 'tuple'>
print(type(tuple))    # <class 'type'>

test_str = "I love python"
print(type(test_str))  # <class 'str'>
print(type(str))  # <class 'type'>
Copy the code
  • Value: Data represented by an object
print(a)  # 3
print(test_list)  # [1, 2, 3, 4, 5]
print(test_str)    # I love python
Copy the code

“Identity”, “type”, and “value” are assigned when all objects are created. These three features exist as long as the object exists.

conclusion

In fact, the software framework we are studying is a collection of superclasses. The framework is the implementation of common programming tasks into classes. All we need to do is write our own subclasses to combine and customize the debugged code. In addition, we categorize common OOP structures, called design patterns, to help solve design problems.

These software frameworks may provide some database interfaces, test protocols, GUI toolkits, and so on

This article provides some conceptual introductions to classes and object-oriented programming, giving us a glimpse of the OOP utopia.

Reference books for a series of articles:

  1. Python Learning Manual (5th edition)
  2. Object-oriented Analysis and Design (3rd edition)
  3. Python Cookbook(3rd edition)