[Tutor] OOP, classes ,

Mats Wichmann mats at wichmann.us
Mon Oct 3 14:22:04 EDT 2022


On 10/3/22 04:30, Mert Oner via Tutor wrote:
> Hello  tutors,
> 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.
> Could you please help me out in layman terms? Or maybe some easy to understand study metarials.
> I really appreciate you guys, taking precious time from your daily life to help us beginners.
> Peace!


Boy, what an easy question :)   I bet the book will descriibe this 
somewhere...  and the author will have spent lots of time thinking about 
that, so will probably have a more comprehensive description than we can 
fire off here.

A Python class definition is your opportunity to define a new object 
type (data type, if you prefer), analagous to predefined types like 
integer, or list, or dictionary, which are other datatypes.  It could be 
as simple as a couple of data elements (modern Python has a shorthand 
notation for such, called dataclasses), or it can add in functions which 
operate on the internals of that datatype - called methods.

Inheritance simply means you aren't writing your whole custom data type 
from scratch, you reuse parts of an existing class. This relationship is 
characterized by the term "is a", e.g if you start with a UserDict from 
the Python standard library and write your own class MyDict, you can say 
MyDict Is A UserDict - but with special features.

class MyDict(UserDict):
     ....

Composition is where one class can contain an instance of another. This 
relationship is often expressed as Has A.  You actually do this a lot 
without knowing it:

class MyClass:
     def __init__(self):
         self.data = {}

The "data" attribute is initialized to an empty dictionary - so it "has 
a" dict.

Beyond that maybe it's better if you ask actual questions?




More information about the Tutor mailing list