Sitemap

Python — Basic Review before diving deeper 05

Want to recall the memory of Python easily?

Press enter or click to view image in full size

This is the note taken while I learned Python “again”, hope it helps you too.
There are additional links in the article for you to understand further information.

Quick tip |
Find ⚡for each topic

☘️Directory

  • OOP(Object-Oriented Programming)
  • Exercise 08 — The most expensive price
  • Classmethod vs. Staticmethod
  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism
  • Exercise 09 — Cat Inheritance
  • Super

❤️Good Resources

Bro code | Python full course
ZeroToMastery | Pyhton
Codes in Github

⚡OOP

Complete Explanation | GeeksforGeeks

🔸Encapsulation, Abstraction, Inheritance, Polymorphism are four pillars in Object-Oriented Programming

What you should know,

🔹class — a collection of objects

🔹__init__ is similar to constructors in C++ & Java, always executed when the class is being initiated

🔹self — represents the instance of the class — More in GeeksforGeeks

Press enter or click to view image in full size

The result shows info of the object person. And 0x7f6f682371f0 is where the object located in the memory.

Press enter or click to view image in full size

Add a method in class.

Press enter or click to view image in full size
Press enter or click to view image in full size

More attributes are available too.

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

❗Class Object attributecan be called by object name or self.

Press enter or click to view image in full size
Press enter or click to view image in full size

Method in Class, it looks like this.

Press enter or click to view image in full size

Can you replace object name — person with self?

Press enter or click to view image in full size
?
Press enter or click to view image in full size
No😮

The most expensive Price

Exercise 08

Press enter or click to view image in full size
Press enter or click to view image in full size

⚡classmethod & staticmethod decorator

Which is the method can be called on the class without instantiating it into an object.

🔹A class method can access or modify the class state while a static method can’t access or modify it.

Get Yen Wang’s stories in your inbox

Join Medium for free to get updates from this writer.

🔹Generally use static methods to create utility functions.— GeeksforGeeks

🔸classmethod

Press enter or click to view image in full size
Press enter or click to view image in full size
😮
Press enter or click to view image in full size

🔸Staicmethod

Press enter or click to view image in full size

⚡Encapsulation

It encapsulates all the data(functions, variables…) into a big object.

Press enter or click to view image in full size

⚡Abstraction

An abstract class can be considered as a blueprint for other classes.

A class which contains one or more abstract methods is called an abstract class. An abstract method is a method that has a declaration but does not have an implementation.— GeeksforGeeks

Press enter or click to view image in full size

⚡Inheritance

More in W3Schools

🔹Allow a class that inherits all the methods and properties from another class.

🔹Parent class also called base class(The inherited class)

🔹Child class also called derived class(The class inherited from parent class)

Less development and maintenance expenses result from an inheritance — GeeksforGeeks

Press enter or click to view image in full size
Press enter or click to view image in full size

This is not right.

Press enter or click to view image in full size
Press enter or click to view image in full size
❌
Press enter or click to view image in full size
✅
Press enter or click to view image in full size

⚡Polymorphism

🔹Means “many forms”

🔹With polymorphism, we can execute same method for other classes

Press enter or click to view image in full size

Actually there is a Drier way to serve the walk method. As you see, I use walk() method three times which are not really DRY(Don’t repeat yourself).

Have a look at the Exercise 09 — Cat Inheritance.

⚡Cat Inheritance

Exercise 09

Press enter or click to view image in full size

😮⚡⚡Do you see the beauty of this?

⚡Super

More in GeeksforGeeks

🔹Inherit all the methods and properties from another class

How Inheritance works without super()?

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

We encounter the error that both class has __init__ constructor trying to call name_ & name, but a class didn’t have name attribute.

A simplier way

to understand the structure for this kind of situation.

Press enter or click to view image in full size

Now, we can go back to the correct version.

Press enter or click to view image in full size
Done 🙆‍♀️

Written by Yen Wang

Curious mind. Interested in both creative and exploitative sides. More about me | https://www.yenwang.dev/