Why is object oriented programming useful?

  • Python
  • Thread starter member 428835
  • Start date
  • Tags
    Classes
Person(34, 8888)person2 = Person(78, 777)person3 = Person(23, 774)Using objects, we only have to create 2 variables for each person instead of 2 x N, which can save a lot of code and make it much easier to manage and manipulate the data.
  • #1
member 428835
I'm looking at a script and it begins with
Python:
class Solution(object):
   def lengthOfLongestSubstring(self, s):
but what is the point of line 1? I've tried reading, and am pretty confused. Also, what is the purpose of "self" in the function? At some point later the script writes
Python:
ob1 = Solution()
print(ob1.lengthOfLongestSubstring("ABCABCBB"))
What's happening here with the ob1 = and then the ob1.?
 
Technology news on Phys.org
  • #2
If you want the EE version / history of OO programming then here goes:

Its all about organizing your data and your code into a modular structure not unlike how electronic circuits were organized into ICs. ICs have specific agreed upon input / output / control lines. EEs would conform to and follow the IC spec sheet to create ever more complex circuitry. They didn't need to worry how the IC worked internally which allowed IC designers to improve the IC while keeping the pinouts the same.

Procedural programs like C-based ones relied on using arrays of structs to organize your data and simple function calls to process that data. However while structs are useful in organizing the data, creating a class with the struct data and with relevant methods to handle the data is even more useful. A class becomes a kind of software IC that can encapsulate and hide details not needed by the application programmer, making a kind of separation between the application programmer and the class designer programmer.

Hence, OO swept the programming world leaving us where we are today with programs that are arguably more complex but that have a greater scalability for growth and new features.

If you think objects you become a more balanced programmer with better organized code which is a paragon of beauty (not :-) ).
 
  • Like
  • Informative
Likes PeroK, sysprog, jim mcnamara and 1 other person
  • #3
joshmccraney said:
I'm looking at a script and it begins with
Python:
class Solution(object):
   def lengthOfLongestSubstring(self, s):
but what is the point of line 1? I've tried reading, and am pretty confused.
Any programming language that supports object-oriented programming (OOP), such as C++, C#, Java, Python, and others, has a class construct. The purpose of a class is to encapsulate some kind of data, such as a solution in your example, in an object, and provide functions or methods for that object. So a class encapsulates both the data of concern, and well as the functionality needed to manipulate that data.

Rather than have lots of functions at the global scope, member functions generally must be called through an object. For example, the lengthOfLongestSubstring() member function can't be called on its own. It must be called through a Solution object, as in the example below.
joshmccraney said:
Also, what is the purpose of "self" in the function?
Every object (an instance of some class) provides some mechanism in each member function to provide access to the specific object. Python calls this self. C++'s version is called this.
joshmccraney said:
At some point later the script writes
Python:
ob1 = Solution()
print(ob1.lengthOfLongestSubstring("ABCABCBB"))
What's happening here with the ob1 = and then the ob1.?
The first line creates an instance of the Solution class; i.e., an object named ob1. The second line calls the lengthOfLongestSubstring() member function of ob1. as an argument to print().
 
  • Like
Likes sysprog and jedishrfu
  • #4
A great motivation for classes is that it is a way to have several things in the same class or in different classes that each knows how to create itself and delete itself and keep track of how it should behave and what data it should retain and report.
This can be a nuisance if it is not necessary, but a great help other times.

Imagine a computer simulation of several types of vehicles entering and leaving an intersection. There can be classes for cars, trucks, emergency vehicles, motorcycles, bicycles, pedestrians, blind people, etc. They can all behave appropriately in the simulation without the top-level program keeping track of all the similarities and differences.
 
  • Like
Likes symbolipoint and sysprog
  • #5
Thanks everyone, it's starting to make much more sense!
 
  • Like
Likes berkeman
  • #6
joshmccraney said:
Thanks everyone, it's starting to make much more sense!
Persistence -- if you don't get it instantly, please keep trying for a while -- please don't give up too easily . . .
 
  • #7
Mark44 said:
Every object (an instance of some class) provides some mechanism in each member function to provide access to the specific object. Python calls this self. C++'s version is called this.
And in VB.NET, it's "me"
 
  • #8
Mark44 said:
Python calls this self.
Actually:
https://www.w3schools.com/python/gloss_python_self.asp said:
It does not have to be named self , you can call it whatever you like, but it has to be the first parameter of any function in the class
 
  • Informative
Likes berkeman and jedishrfu
  • #9
Data abstraction refers to providing only essential information to the outside world and hiding all the background details used in methods.

I must have missed this in the discussion: an important goal -- data abstraction.

Data abstraction enforces a clear separation between the abstract properties of a data type and the concrete details of its implementation. The abstract properties are those that are visible to client code that makes use of the data type—the interface to the data type—while the concrete implementation is kept entirely private, and indeed can change, for example to incorporate efficiency improvements over time. The idea is that such changes are not supposed to have any impact on client code, since they involve no difference in the abstract behavior.
-- wikipedia
https://en.wikipedia.org/wiki/Abstraction_(computer_science)#Data_abstraction
 
  • Like
Likes Tom.G
  • #10
jim mcnamara said:
Data abstraction refers to providing only essential information to the outside world and hiding all the background details used in methods.

I must have missed this in the discussion: an important goal -- data abstraction.-- wikipedia
https://en.wikipedia.org/wiki/Abstraction_(computer_science)#Data_abstraction
I think the focus on abstract definitions vs concrete implementations in that article is something different from what we are talking about here (for instance what does a concrete implementation of a member property look like?).

I think it is better to consider abstraction as an extension of encapsulation such as in the section Abstraction in object oriented programming.
 
Last edited:
  • Like
Likes jim mcnamara and sysprog
  • #11
Imagine that you have data from 100 people.

You have one option:

Create new variables for each one:

age_person1 = 34
phone_person1 = 8888

age_person2 = 78
phone_person2 = 777

age_person3 = 23
phone_person3 = 774

That is 2 x N variables.

Compare with object oriented. We know all people have a phone and age so we just create a new object with those 2 variables:

class person(object):
__init___(self, age, phone):
self.age = age
self.phone = phone

Now you don't need to create 200 variables, you just instantiate the objects which already contain the variables

person1 = person(34,8888)
person2 = person(78,777)
...

Now there is no need to repeat the process of variable creation because indeed it is redundant.

Self is simply a word to refer to whatever name you give to the object. Instead of self you can use any other word.

For me this example is what best motivates the need for object oriented programming.
 

FAQ: Why is object oriented programming useful?

What is a class in programming?

A class in programming is a template or blueprint that defines the characteristics and behaviors of a particular type of object. It contains data attributes and methods that can be used to create multiple instances of the same type of object.

Why do we need classes in programming?

Classes allow for code reuse and organization, making it easier to maintain and modify code. They also help with encapsulation, allowing for data to be kept private and only accessed through methods defined in the class.

How are classes used in object-oriented programming?

Classes are a fundamental part of object-oriented programming. They are used to define the structure and behavior of objects, which are instances of a class. This allows for a more modular and flexible approach to programming.

What is the difference between a class and an object?

A class is a template or blueprint that defines the characteristics and behaviors of a particular type of object. An object is an instance of a class, meaning it is created from the class and has its own unique set of data and behaviors.

Can you give an example of a real-world use case for classes?

A real-world use case for classes could be in a banking system. The class "Account" could define the attributes and methods for a bank account, such as balance and withdraw/deposit functions. Multiple objects could then be created from this class, representing different customers' accounts.

Similar threads

Replies
2
Views
996
Replies
2
Views
2K
Replies
23
Views
2K
Replies
11
Views
1K
Replies
9
Views
2K
Replies
21
Views
2K
Back
Top