Why is object oriented programming useful?

  • Context: Python 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Classes
Click For Summary
SUMMARY

The discussion centers on the utility of Object-Oriented Programming (OOP), particularly in Python, as exemplified by the class structure in the provided script. The class encapsulates data and methods, allowing for modular programming similar to integrated circuits in electronics. Key concepts include the role of "self" in class methods, which provides access to instance-specific data, and the importance of data abstraction in separating interface from implementation. This modular approach enhances code organization and scalability, making it easier to manage complex systems.

PREREQUISITES
  • Understanding of Python class constructs
  • Familiarity with the concept of encapsulation in programming
  • Knowledge of data abstraction principles
  • Basic programming skills in any OOP language (e.g., C++, Java, C#)
NEXT STEPS
  • Explore Python class inheritance and polymorphism
  • Learn about data encapsulation and its benefits in software design
  • Study the principles of data abstraction and how to implement them in Python
  • Investigate design patterns that utilize OOP concepts for better code organization
USEFUL FOR

Software developers, particularly those working with Python, educators teaching programming concepts, and anyone interested in improving code organization and scalability through Object-Oriented Programming.

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
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   Reactions: PeroK, sysprog, jim mcnamara and 1 other person
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   Reactions: sysprog and jedishrfu
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   Reactions: symbolipoint and sysprog
Thanks everyone, it's starting to make much more sense!
 
  • Like
Likes   Reactions: berkeman
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 . . .
 
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"
 
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   Reactions: berkeman and jedishrfu
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   Reactions: 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   Reactions: 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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 34 ·
2
Replies
34
Views
5K
  • · Replies 43 ·
2
Replies
43
Views
4K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 9 ·
Replies
9
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
13
Views
2K