We have set up the learning path and the most particular features of python.
We are looking for efficiency and a quick glossary-type to retrieve and process the task or information.
dir(class_ name or module_name)
# return all the avalaible methods ,property and class avalaible
def increment(number,by=1):
return number + by
# it's also possible case
def increment(number,by=1):
return(number,number + by)
increment(2,3)
>>>> (2,5)
# possible type annotation (doesn't make it statistically type nevertheless)
def increment(number: int,by=1: int) -> tuple:
return(number,number + by)
(2,5)
is an instance of a tuple class (or object). A tuple is a read only list. It's iterable.
Built-in fonctions : search built-in fonction python 3 (we'll get them all)
Python has iterable !!! remember that
reverse
test = ["a", "b"]
test[::-1] # reverse
test = "ab"
test[::-1] # reverse
# dictionary looks complex. it's really a dictionary comprehensions
# come back later
We have some built-in function capable of generating object instance from a given class
- list()
- tuple()
- set()
- dict()
- They are factory methods
- object()
- isinstance()
- issubclass()
Get the size of an object
from sys import getsizeof
# Generator
values = (expression for item in iterable_object_large)
getsizeof(values) -> much smaller
- getting started
- python implementations
- How python code is executed
- Basics
- Variables: all assignment and declaration possible
- dynamic typing: Python doesn't have static typing
- type annotation: it's possible to work with mypy, buit still dynamic
- Mutable and Immutable types: memory allocation are different
- String: each strings is an instance of the clrass str
- Escape Sequences: multiple line string with triple quotes
- Formatted Strings: string interpolation,
interpolation = 10 f"{interpolation}"
- Usefull string methods: Inherited from the class str
- Numbers: int, float, binaries, hexodecimals, complex Numbers
- Arithmetics Operators: the basics 4
(+,*,-,/)
,//
return the floor part of the division results, modulo%
', power**
,x(+,*,-,/,//,%,**)= 1
- Working with Numbers: no constant in python it's just a naming convention, usefull built-in fonctions, the
math
module - type conversion:
int(), float(), bool(), str()
, Falsy values - conditional statements: if, else, elif
- Ternary Operator:
message = "Eligible" if age > 18 else "Not Eligible"
- For Loops:
for x in str_instance:
,for x in list_instance:
,for x in range(number):
range(number) -> 0,...,number-1 = instance of range class,for key in dictionay_instance:
a dictionary is iterable on a multiple way. - For Else:
for var in **iterable_instance: <block><break> else: <block>**
- While Loops:
while <condition>:
you can had a else clause that will be executed when the while loop is done. - Functon:
def function_name(parameter_list): pass
, a function always a default return value, understand thereturn
keyword of a function, type annotation is possible if you use mypy - Arguments xargs: Multiple arguments,
def mutliply(*parameters):
*parameters, all the arguments we will be put inside a tuple, A tuple is iterable (read-only) - Arguments xxargs: A variation of the concept of Multiple arguments,
def user(\**user):
, now we can add multiple keyword arguments, it return dictionary with the parameters as the keys and their arguments, a dictionary is iterable - Scope: global scope, function scope, class scope, the
global
keyword make sure you avoid the possible side effects - debugging:
pdb class
, or use the ide/code editor debugging system - vscode trick for mac
- fizzbuzz test
- Python Data Structures (list , tuples, set and dictionaries)
- List:
[]
is an instance of List class,[0]*5 -> [0,0,0,0,0]
,[] + [] -> []
, list functionlist(range(number))
- Accessing Items: indexing with square bracket notation just as strings
- Unpacking List:
num= [] \n a,...,c = num
,a, b = [1,2,3] -> a =1 && b=[2,3]
- Looping over List: use an enumerate object
for index, values in enumerate(list_object): ->(index, value)
, iterable, enumerate will return a tuple that we unpack in the index and value - Adding or removing items: use the
list_object.methods
to remove or add items,del
keyword - Finding Object in a list: use the a list method to look for an object based on a conditional
- Sorting List: use the list method
list.sort()
,sorted()
built-in,list.sort()
is a higher-order function && The key will accept the callback function - Lambda function: anonymous function, very cleaner way to add as a callback function in an higher-order function,
list_instance.sort(key= lambda params:expression)
, we are telling python how to sort the given list if he can't - Map function: another higher-order function,
map(lambda params:expression, list_instance)
, return a iterable map object,list(map(..))
- Filter function: return a iterable filter object,
filter(lambda params:expression,list_instance)
, higher-order function,list(filter(..))
- List Comprehension: 1)
[expression for items in list_instance]
and 2)[item for item in list_instance if expression]
- Zip function:
zip([],[]) -> [(), ...,()]
, return a iterable zip object,list(zip(..))
- Stacks: particular topic in a data structure and algorithm course, LIFO,
list_instance list_instance.append() list_instance.pop()
redirect to the lastlist_instance[-1]
, if emptyif not list_instance: <code block>
we looking for a falsy0, "" or []
- Queues: another topic in Data structures and algorithm, FIFO, list_instance can be ineffective,
from collections import deque
andqueue = deque([])
, we have more methods likequeue.popleft()
orqueue.appendleft()
- Tuples: read only list,
point = (1,2)
orpoint= 1, 2
point is an instance of class tuple,() + () -> ()
,tuple(iterable) -> tuple_instance
, you can't modify a tuple but you can use concatenation to add element, unpacking is workingx, y = points -> x =1 and y = 2
- Swapping Variables:
a, b = b, a
but it's really unpacking a tuplea,b =b,a <=> a, b = (b,a)
- Arrays: Dealing with large number,
from array import array
, it has a lot the list_class methods,array.array("i",list_instance)
"i" is the typecode indicating the types - Sets: collection with no duplicate data, bitwise operator,
first={1,2,3} second={1,4} first | second -> {1,2,3,4}
,first & second -> {1}
, other possible operationfirst - second
first ^ second
, They are unOrder collection no INdex, still possible to iterate and test for existence - Dictionaries: a collection of key-value pair, the key must be immutable types but the value can be of any types,
{"x":1,"y":2} <=> dict(x=1,y=2)
, mutable,point[key] <=> point.get(key)
, iterate over dictionaryfor key, value in point.items(): <Block>
- Dictionary Comprehensions: set comprehension
{expression for item in iterable_object}
, dictionary comprehension{x: expression for item in iterable_object}
, tuple comprehensions leads to a generators object(expression for item in iterable_object) -> generator Object
- Generator Expressions: Problem dealing with large streams of data, generator objects are iterable,
(expression for item in iterable_object) -> generator Object
, the size a generator will be consistant even if the iterable becomes more large - Unpacking Operator: similar to the spread operator in javascript,
n=[] && b = [] => c = [*n, *b] -> []
, we can unpack any iterable, we can unpack a string, unpacking dictionary has a particularity - Most repeated character test
- Exceptions
- Exceptions: Error that terminate the execution of a program, Handeling exceptions to prevent program from crashing
- Handeling Exceptions:
try: <code block> except Built-in-Exception-object: <code block concerning the error> <any other code block wibe executed despite the error>
- Handeling different exceptions: There is a module containing all the class exeptions,
try: <code block> except Built-in-Exception-object: <code block concerning the error> except another-B-in-Exception-Object:<code block for the second exception> <any other code block wibe executed despite the error>
, A better way to handle mutiple types of exceptionstry: <code block> except(Exception_classA, Exception_ClassB): <code block for exceptions> <the rest of the program
- Cleaning Up: some case scenario we need to add add a finally clause,
try: code except ErrorClass: code else: code finally: code
- The With statement: work with certain type of object, when you are working with files scenario, object (class) that have context management protocol
class_instance.__enter__ class.__exit__
we can work with the with statement - Raising Exceptions:
raise
statement to raise an exception,raise Built-in-ErrorClass("error message")
, raise by itself is costly and he did prevent the programm to crash - Cost of Raising Exceptions: Costly,
from timeit import timeit
time of execution of the code, it cost in efficiency of the run time, Raise exception if you really have to
- Classes, status:watch
- Classes: allow us to create object with methods and attribute, It is a blueprint to create new object
- Creating Classes:
class NameOfClass: pass
,class NameOfClass: def myfun(self): pass
, seems like the file containing a class is considered a module, create an instanceinstance = NameOfClass()
, built-inisinstance(instance, NameOfClass)
to check,type(insctance) -> <class __main__.NameOfClass>
- Constructor: set initial values for our class,
__init__
is the magic method who is the constructor,class NameOfClass: def __init__(self,valueA, valueB): self.valueA = valueA self.valueB = valueB <other methods or behavior of the class>
- Class VS Instance Attributes: instance attributes are avalaible for the instances, class attributes are only for the class, class attributes are share for all instances code_below
- Class VS Instance Methods: mostlikely the same philosophy, factory methods are class methods that create objects
- Magic Methods: you can see the complete list of magic methods online, use the
dir(class or instance_class)
to see the behavior and magic mehtods avalaible,num = 10 num.__add__(100) -> 110
,num.__str__() -> "10" <=> str(num)
- Comparing Objects: even if the objects where initialize the same way they will haev their own reference in memory, comparison magic methods will help with this purpose, we can override the magic method with we want to, most of the magic methods aren't implemented,
class NameOfClass: def __magicMethods__(self,<...>): <code block>i
, implement theNameOfClass.__gt__()
and python will know how to deal with less than, go on the documentation to know all the comparison operator - Performing Arithmethic Operations: depending of the class you are using - you will mostlikely implementing the beahvior yourself
def __add__(self, nameofinstance): return NameOfClass(self.varA + nameofinstance.varA, ..., self.varN+ nameofinstance.varN)
- Making Custom COntainers: make a container for blogue content handle by a class , inside the file tagcloud.py, best practice to use you own class when the given data structure won't gives you the features that you need, nice exercice to see how powerful magic methods are
- Private Members: i already make the construct attributes private, we have the concept for private methods and attributes to hide somme information, access them is dfficult,
instance_class.__dict__
holds all the attributes of a class even the private one,cloud.__dict__ -> {'_TagCloud__tags': {'python': 51, 'r': 1, 'latex': 3, 'rdms': 1, 'nrdms': 1}}
andcloud._TagCloud__tags -> {'python': 51, 'r': 1, 'latex': 3, 'rdms': 1, 'nrdms': 1}
, they are still accessible - Properties: control over an attributes inside a class, we know that the
__init__
automatically is a getter and setter, but we can implement our own getter and setter with a property, an object that help us set or get an attributesproperty()
, a decorator@property
- Inheritance: we know that we have the root class, A class can inherite from another class, Parent class / child class, parent class
class Animal:
child classclass Mammal(Animal):
- The Object Class:The object class is the base class for all object from python, built-in function to create an object
object()
- Method Overriding: overriding or replacing a method in the base class,
super()
- Multi-level Inheritance: Avoid multi-level inheritance, a good limitation is to 1 or 2 level
- Example of Inheritance: not much than 1 or 2 level, Avoid method overriding
- Abstract Base Classes: learn to identify abstract concept,
from abc import ABC, abstractmethod
, we can't extenciate an abstract class - Polymorphism: Many forms situation
- Duck Typing: something about python being dynamic, you can achieve polymorphism without abstraction
- Extending built-in types: we get the built-in objects,
class Text(str):
etclass TreackableList(list):
- Data Classes: class without behavior, class dealing only with data,
from collections import namedtuple
namedtuple()
inside the collections modules -> an object class, nametuple is better suited to work with data class
- Modules, status:watch
- Creating Modules: A module is a file that contain some python code, naming convention
lowercase_lowercase_...
,from name_module import functionOrClass -> functionOrClass
, orimport name_module -> name_module.functionOrClass
- Compiled python files:
__pycache__
will be created because of the module interaction we introduce, to speed up module loading - Modules search path: an array of all possible path look by python
import module_name import sys print(sys.path)
- Packages: a package is a container for one or more modules,
import package_name.module_name -> package_name.module_name.functionOrClass
, a package must have__init__.py
,from package_name.module_name import functionOrClass -> functionOrClass
,from package_name import module_name -> module_name.functionOrClass
- Sub-packages: we can break down package to subpackages,
__init__.py
is also important,from package_name.subpackage_name.module_name import functionOrClass -> functionOrClass
,from package_name.subpackage_name import module_name -> module_name.functionOrClass
- Intra-package references:
from package_name.subpackage_name import module_name <=> from ..subpackage_name import module_name -> module_name.functionOrClass
- The dir function: get the list of method and attributes from a given object,
dir()
,module_name.__name__
,module_name.__package__)
,module_name.__file__
- Executing Modules as Scripts:
if __name__ == "__main__"
- sort |> list_instance.sort() and sorted
- map
- filter
- reduce
- sum
- any
- all
- List comprehensions
- use instead 1) of
list(map(..))
- use instead 2) ofr
list(filter(..))
- dictionary comprehensions
- tuple comprehensions leads to generator object
- set comprehensions
- Stacks
- Last In First Out
- can use a list_instance
- deque can also do stacks
- Queues
- First In First Out
- can use a list_instance
- deque object to avoid memory problem
- deque is from the collections module
- collections has a lot of good class (Defaultdict, Counter, Deque, Namedtuple, ChainMap, OrderedDict)
- Performance problem
- Class built very similarly with The list class
- The type code has special meaning
- we can unpack any iterable
- we can unpack a string
- dictionary scenario:
first = dict(x=1)
second = dict(x=10,y=2)
# unpacking use **dict_instnace in this case
combo = {**first,**second, "z"=1}
combo
# -> {'x': 10, 'y':2, 'z':1}
# but we woull get an error if we use the built-in dict()
combo = dict(**first,**second,z=1)
combo
# -> TypeError: type object got multiple values for keyword argument 'x'
- the value of the key x has change in the first scenario
- class capable of context management protocol
- To be clear -> class that have two magic methods
class.__enter__()
andclass.__exit__()
- The with statement work for these objects
Remember to understand how to set proper association in python
Object Oriented Paradigm:
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
- Constructor is use to initialize an object from a class
- Maybe a class inside a python file automatically makes this file a module?
- Magic Methods avalaible for each Classes in python
- Class has methods or attributes, we call that behavior
- Instance variables, class variables, class function, instance function, private variables, private functions
- Seems that instance attributes and class attributes are getters and setters (automatically)
- read the documentation on the magic methods, most of them are inherited
- Most of the magic methods aren't implemented, you have to do that
- We don't really have a strict concept of private members
- Set our own getters and setters with a property object
property()
- cleaner way, use a decorator to set the getter and setter
@property
- Inheritance, method overriding, astract class
- Polymorphism
class Point:
# class attributes
default_color = "red"
# constructor that define instance attributes automatically
def __init__(self,x,y):
self.x = x
self.y = y
- class Attributes are share accross all instances
point = Point(1,2)
point2 = Point(3,4)
point.default_color
will output the same aspoint2.default_color
- because It's a class Variable we can do
Point.default_color
class Point:
# clase attributes
default_color = "red"
# constructor that define instance attributes automatically
def __init__(self,x,y):
self.x = x
self.y = y
# class methods
@classmethod
def zero(cls):
return cls(0,0)
# same as Point(0,0)
# instance method
def draw(self):
return f"point du repere: {self.x} and {self.y}"
- class methods can helps us define factory methods
zero = Point.zero()
will create an object initialize with 0 and 0class __main__.zero()
zero is considered a factory methods- I can conclude that the built in function generating objects are factory methods
A proper way to work with method overriding
class Animal:
def __init__(self):
self.age = 1
class Mammal(Animal):
def __init__(self):
super.__init__()
self.weight = 2
Here, we are properly overriding the constructor. with super()
we have access to the methods, attributes and the constructor inside the based class. Now we make sure that the based constructor is not comletely override.
Hierachy
- pypi is where libraires are stored
- package :
__init__.py
, contains modules and subpackages - subpackage:
__init__.py
, include inside package, contains modules - Modules: include package, include subpackage, contains class, contain functions
- class: include inside modules, contains attributes and methods (functions)
- function: include inside class, include modules, contains block of codes