Object Oriented Programming in Python: Classes

5731 단어 pythonOOPOOP

Object Oriented Programming (OOP)

computer programming model that organizes software design around data, or objects, rather than function and logics

By constructing model with data and objects, OOP allows the programmer to create new classes that the model needs to solve the problem.

Why Use OOP?

Imagine yourself running a start-up which stores the information of applications submitted to firms.

To store the information, your start-up receives three types of information - applicant name, company and college major.

name = 'John Lennon'
company = 'Facebook'
major = 'Computer Science'

Now, we will create a function which receives information above as parameter and neatly prints the given inputs

Input

def application_info(name, company, major):
    print('----------------------------')
    print('Name : ', name)
    print('Company  ', company)
    print('Major : ', major)
    print('----------------------------')

application_info(name, company, major)

Output

----------------------------
Name :  John Lennon
Company   Facebook
Major :  Computer Science
----------------------------

Now, you are start-up is finally having momentum and acquired another customer.

name2 = 'Paul McCartney'
company2 = 'Amazon'
major2 = 'Math'

application_info(name2, company2, major2)

Output

----------------------------
Name :  Paul McCartney
Company   Amazon
Major :  Math
----------------------------

As for now, you do not have much concern as there are only two customers. However, alongside the growth in customer size customers will demand richer functionality and higher speeds. We may simply "add" new features by creating new functions - however is there a better way?

Class

Luckily, we have class to make everything easier. Classes provide a means of bundling data and functionality together. By bundling related data and functions within a class, programmers will be able to easily manage the system and make adjustments.

Let us first skim through the code.

Input

class ApplicationInfo:
    def set_info(self, name, company, major):
        self.name = name
        self.company = company
        self.major = major 

At this moment, it is bit too early to explain what self is. For simplicity, remember that the first parameter of class's method must be self.

As we can see from the code, self.name , self.company, and self.major are binded into the inputs sent to the class.

Input

application1 = ApplicationInfo()
application1

Output

<__main__.ApplicationInfo at 0x7fc471017110>

Class ApplicationInfo contains method set_info(), hence we will call the main method through instance application1.

Input

application1.set_info('Freddie Mercury', 'Google', 'Engineering')

print(application1.name, application1.company, application1.major)

Output

Freddie Mercury Google Engineering

Format of the output is not as neat as we wish it to be. Let us now add new method inside the class to make things neater.

Input

class ApplicationInfo:
    def set_info(self, name, company, major):
        self.name = name
        self.company = company
        self.major = major 
    
    def print_neat(self):
        print('----------------------------')
        print('Name : ', self.name)
        print('Company  ', self.company)
        print('Major : ', self.major)
        print('----------------------------')

And also make another instance containing different applicant information.

Input

application2 = ApplicationInfo()
application2.set_info('Noel Gallagher', 'Netflix', 'Statistics')
application2.print_neat()

Output

----------------------------
Name :  Noel Gallagher
Company   Netflix
Major :  Statistics
----------------------------

By calling print_neat() method, the output looks much more organized.

So far, we have created two instances application1 and application2. Although both instances contain identical instance varibales, each instance is binding different data.

Therefore, we can consider constructor as a structure of code which transforms individual ingredients to a complete product.

In other words, constructor is a waffle machine which turns waffle dough into a waffle (instance).

좋은 웹페이지 즐겨찾기