or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
<style>
tags:
Week 17
W17D4
Python Imports, Decorators, and Classes
Week 17 Day 4
Review from yesterday…
Today's Topics
The Python Import System
To import code from a module, we use the
import
keyword. The import keyword will locate and initialize a module, and give you access to the specific names you have imported in the file.There are no exports in Python! Anything we define in a module/file is automatically available for import.
The
import
keywordThe Python standard library has a number of packages you can import without having to install them. Let's use the
random
package as an example (this would work the same with any package).Use the
as
keyword to alias package/object names.You can also import specific objects from a package using the
from
keyword.Import Python code from another file
If two files are at the same level, import using the filename (without the
.py
extension).If we need to specify a path to a file, we use dot notation:
__init__.py
This file should go in any directory being imported. It will transform a plain old directory into a Python module/package. It can be completely empty, and often will be!
Upon import from a module/package, its
__init__.py
file is implicitly executed, and all objects it defines are bound to the module's namespace (documentation).Import Practices (30 min)
Python Import Short Practice - 15 min
Decorators
What's a decorator?
A decorator is a function that takes in another function as a callback and modifies or extends the behavior of the callback function.
Decorators
Let's create a decorator that could be used for timing function calls.
Decorators
Using the
@decorator_name
syntax, we can shorten this:To this:
Decorating a function definition (with the
@decorator
syntax) does the same thing as reassigning the function name to the return value of the decorator.Passing arguments through a decorator
What if I want to wrap functions that take arguments… but I want to be flexible about what kind of arguments the function takes?
Decorator Practices (35 min)
Decorators Quiz - 5 min (in your howework)
Hello World Decorator - 3 min
Order Decorator - 3 min
Timer Decorator - 10 min
Chain Decorator - 10 min
Classes
To create a class we use the
class
keyword, and by convention, we capitalize the names of classes.Python's constructor method is called
__init__()
.Instances of classes
We create instances of a class by invoking the class as though it is a function (this invokes the class's
__init__()
method).Wait, what is
self
?self
refers to the instance that a method was called on.Whenever you invoke an instance method on a class instance, it is as though you are invoking the class's own method, and passing in the instance as an argument.
Instance variables and methods
You can set attributes on the instance with dot notation (
self.some_attribute = value
).You can add instance methods to the class by defining functions and passing in
self
.Class variables
Class variables are not attached to
self
. They are available for access on the class itself and across instances.If we update a class variable on an instance, a shadow instance variable is created that hides the class variable of the same name.
Class methods
We can use the
@classmethod
decorator to write class methods.The first argument will refer to the class itself (conventionally called
cls
), rather than an individual instance.Static methods
Static methods don't take implicit arguments—they can't access the class or any instance of it.
Getters and setters
Getters & setters allow us to have methods that behave like properties.
They provide a convenient interface for implementing more complicated logic necessary for getting/setting a class property.
They can also be useful for protecting "private" values on your class.
Getters
A getter allows you to define a method that behaves like a readable property. The
@property
decorator over a method creates a getter.While the getter is a function, it is invoked as if it were a property.
Setters
A setter allows you to define a method that updates the getter "property". The decorator used to create a setter is
@<getter_method_name>.setter
.You can have a standalone getter, but you must have a getter in order to have a setter. The setter method runs when you change the getter "property."
Basic Class Syntax Practice (35 min)
Bad Calculator - 10 min
Getters and Setters - 10 min
Regular Polygon - 15 min
Tree Traversal - Challenge - 15 min
Inheritance
To inherit from another class, we pass a reference to that class as an argument in the class definition.
We can use the
super()
function to get a reference to the parent class, then invoke the desired function.Polymorphism
In OOP, polymorphism allows us to have methods/attributes that behave differently for different classes.
Polymorphism is tied to inheritance. When a child class inherits from a parent class, that child class can override methods from the parent class.
With polymorphism, we can have our
Gadget
class redefine a method from our parentIcon
class:Class Inheritance Practice (30 min)
Quadrilateral with Inheritance - 10 min
Triangle with Inheritance - 10 min
Polymorphism Practices (40 min)
Book Polymorphism - 10 min
Magic Methods - 10 min
Linked List Iterator - 20 min
Long Practice (2 hrs)
Linked List Project - 2 hrs