Python
learning path¶
David J. Malan(@Harvard University): CS50P Introduction to Programming with Python
Achieving CS50P final project: Extract metadata from Instagram
John DeNero(@UC Berkeley): CS61A Structure and Interpretation of Computer Programs
CS61A online textbook: SICP in Python
CS61A soulutions, notes and final project: ...
Pointview of Prof. Hany Farid in the CS61A course
There is this notion that learning happens when you solve a problem. That is completely wrong. Learning happens when you don't solve the problem. The process of failure, the process of struggling, and the process of taking hours to solve something are where the learning is happening.
"Don't be frustrated by things not working. That’s the way it’s supposed to be. That’s what the process of learning is."
Unicode¶
✗的unicode代码是✗
复制✗,然后利用网站,ascii码转unicode,即可得到
Tips from Dr.Deng¶
Material
Notes¶
object: A "bundle" of related attributes (variables) and methods (functions).
You need a "class" to create many objects.
class: (blueprint) Used to design the structure and layout of an object.
class variable: Shared among all instances of a class. Defined outside the constructor. Allow you to share data among all objects created from the class.
Inheritance: Allows a class to inherit attributes and methods from another class. Helps with cold reusability and extensibility.
class Child(Parent)
class Sub(Super)
Attention
In Python, a method is a type of attribute of an object.
When you try to access a non-existent attribute--whether it's a data attribute or a method--Python raises an AttributeError
.
- Methods are callable attributes (bound functions).
- Python doesn't distinguish between "missing method" and "missing attribute" errors -- both trigger
AttributeError
. - This design aligns with Python's dynamic nature (e.g., methods can be added/modified at runtime).
multiple inheritance: inherit from more than one parent class.
C(A, B)
multilevel inheritance: inherit from a parent which inherits from another parent.
C(B) <- B(A) <- A
Abstract class: A class that cannot be instantiated on its own; Meant to be subclassed.
They can contain abstract methods, which are declared but have no implementation.
Abstract classes benefits:
- Prevents instantiation of the class itself
- Requires children to use inherited abstract method
from abs import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def eat(self):
pass
@abstractmethod
def sleep(stop):
pass
super(): Function used in a child class to call methods from a parent class (superclass). Allows you to extend the functionality of the inherited method.
Polymorphism: Greek word that means to "have many forms or faces". Poly = Many. Morphe = Form.
Two ways to achieve polymorphism
- Inheritance. An object could be treated of the same type as a parent class.
- Duck Typing. Object must have necessary attributes/methods.
Duck Typing: Object must have the minimum necessary attributes/methods.
"If it looks like a duck and quacks like a duck, it must be a duck."
Aggregation: Represents a relationship where one object (the whole) contains references to one or more INDEPENDENT objects (the parts).