[Tutor] OOP, classes ,

Alan Gauld alan.gauld at yahoo.co.uk
Mon Oct 3 14:46:38 EDT 2022


On 03/10/2022 11:30, Mert Oner via Tutor wrote:

> I am studying python with the help of a book called ' Learn Python3 
> The Hard Way' by Zed Shaw. I am having a really hard time understanding 
> OOP and classes, grandparent, parent and child concepts in classes and 
> Inheritance vs Comparison.

OOP and classes are two related but separate issues.
Many people find them confusing so don't worry about it,
you are not alone.

OOP is a style of programming where you organise the program
as a set of objects that communicate by sending messages to
each other. This is a very different way of thinking about
program structure compared to the traditional hierarchy of
function calls acting on some central data stores.

Each object can be thought of as a little independant program
processing messages from the other programs(ie objects) around it.
Each object has its own internal data and the only way to read
or modify that data is by sending a message to the object.
Each object has its own method of processing the messages
it receives. Many different objects can be sent the same message
but each object type will respond differently - they have different
methods of handling the same message. This is called polymorphism.

OOP is often implemented within a language by providing a structure
called a class. Such a language is often called Object Based or
Object Oriented. The difference being whether it supports
inheritance(OOPL) or not (OBPL). You can do OOP in either
(or neither!).

A class is a container structure which can hold both data and
functions. Functions can be connected to messages (eg by having
the same name) in which case they are the object's methods. Not every
function in a class needs to be a method (private internal functions
used by the methods for example).

A class is effectively a data type definition (like int, float,
string etc) and it is used to create instances of the type which
are the actual objects used in OOP. But objects can be used in non-OOP
programs too when they become simple instances of a user defined type.
Then they  can be used alongside the built-in types to create hybrid
style programs. Hybrid programs are by far the most common kind seen in
the real world. Pure OOP is only worthwhile for certain problem types
and sizes(generally big complex problems related to real-world objects.)

We use classes and objects to model many different types of object.
They can be real-world physical entities such as cars, vehicles,
animals etc They can be real-world abstract concepts such as
shapes, bank-accounts, calendars etc They can also be computer
science or math data types such as a bag or stack or directed graph etc.

Any concept which has both data (or "state" - alive, dead, active,
visible, hidden etc) as well as operations that alter (or read) that
data can be an object.

That's kind of a layman's description of OOP and Classes/objects.
You need to ask more specific questions for more specific answers.
I mentioned inheritance but deliberately didn't elaborate because
it brings a whole extra set of complications. Best to grasp the core
concepts first.

And then, of course, you need to find out how python chooses to
implement all these core ideas.

For a different approach (with code/examples)you can try reading
the OOP topic in my tutorial(see link below).

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos






More information about the Tutor mailing list