UD - FLASK - PY Basic

1. Main

1.1

if __name__ == '__main__':
    app()

1.2 id

string, bool, number & tuple are immutable; list is muttable

a = []
b = a
c = []
print(id(a), id(b), id(c)) # 4380261704 4380261704 4380259400

1.3 avoid using mutable for default parameters

from typing import List


class Student:
    def __init__(self, name: str, grades: List[int] = []):  # This is bad!
        self.name = name
        self.grades = grades

    def take_exam(self, result):
        self.grades.append(result)


bob = Student("Bob")
rolf = Student("Rolf")
bob.take_exam(90)
print(bob.grades)    # [90]
print(rolf.grades)   # [90]     # Whaaaaaat

fix

2. Basic

3. Func

4. Class

Static methods = just place a method inside a class (Module)

Class methods = factories.

5. Import

6. FP

6.1 Decorator, func with params

6.2 Decorator, decorator with params

one more layer to return decorator

Last updated

Was this helpful?