Object Oriented Programming in Python: Classes
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).
Author And Source
이 문제에 관하여(Object Oriented Programming in Python: Classes), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@jiselectric/Object-Oriented-Programming-in-Python-Classes-4wc0vvdr저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)