Object Oriented Programming with Python
https://www.youtube.com/watch?v=Ej_02ICOIgs&ab_channel=freeCodeCamp.org
-
Classes should have the first letter as a capital
-
anything that starts with
__
is called a "magic method" -
you can assign value to parm by given a default value
def __init__(self, name, price, qty=0):
-
to force a data type use
:
after varible name exampledef __init__(self, name: str, price: float, qty=0):
- fact that we have assigned a default value of an int in
qty
tells python the date type it must use
- fact that we have assigned a default value of an int in
-
assert keyword check what is happening to your expectations
-
asserts allow you to add better error handling easily
-
Class attributes are shared belongs to the class itself
-
instance attribute are only for the instance of that class
-
__dict__
all the attribute for Class level and instance level. This is useful for debugging -
__repr__
is a magic method - representing your objects- it is best practise to name the items the same format as when you call the class return
f"Item('{self.name}, {self.price}, {self.qty}')"
=Item('Phone', 100, 5)
- a special method used to represent a class's objects as a string.
- it is best practise to name the items the same format as when you call the class return
-
@classmethod
using this converts function into a class method -
cls
loads the class itself -
@staticmethod
static mehtod should do some work for you that has some logical connection to a class- a static method is bound to a class rather than the objects for that class
-
Parent and child classes
-
super functions allows us to use functionality from parent class
-
self.__class__.__name__
allows you to see the name of the class in a parent child situation -
Encapsulation - describes the concept of bundling data and methods within a single unit
-
Decorators are like another function that you pre execute before another function
-
Puttig 2
__
infornt of a variable or attribute makes it a "private" atrribute -
@property
decorator makes things read only -
@name.setter
used t change variables that are read only -
Encapsulation - Restricting directed action to attributes of program
using __
- you don't allow access directly to an attribut you modify using a method
- Check
apply_increment and apply_discount
functions
-
Abstraction - only show the nessary atrrib hide the unnessary details from users
- hide unnessary info from instances i.e the email sending example
- use
__
double underscore to hide the functions \ methods
-
Inheritance - Inheritance is the capability of one class to derive or inherit the properties from another class.
- Parent child stuff as above
-
Polymorphism - abilite to have diff scenarios The word polymorphism means having many forms. In programming, polymorphism means the same function name (but different signatures) being used for different types.