Python w/Flask
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    ###### tags: `Lesson Notes` `Python` `OOP` `PythonFlask2021` # Classes - So now we went over what the 4 pillars are and we did use classes but just what are they? - How do they work? - What are they used for? ## Blueprints: - This is how we will need to start thinking of classes. When we were creating dictionaries we were basically creating a class. - Each class has 1st a name to call it by like User, Dog, House - Always capitalized - After that we need to set up attributes. These are things that the instances (each object) have. Like a User has the attributes of a name, age, email for example - Next we have methods. These are things that the instance can do. Like a User can take a walk, have a birthday ## Set up the class: 1. To start we need to declair that we are going to create a class ``` class House: ``` 2. Now we need to know how we will create an instance at least to start with ``` winterHome = House() summerHome = House() ``` 3. Next is to add the attributes. Remember that is what each item will have. ``` class House: def __init__(self): ``` - Before we go to much further it is important to note that each class will always start like this. - Inside the () with self will go things that you will need to declair as you create the instance. - Some attributes will not need to be declaired in each instance as it is always the same or is going to be part of a method. - I will add both now to our House class ```python class House: def __init__(self, name, street, city, state, zip): self.name = name self.street = street self.city = city self.state = state self.zip = zip self.floors = 2 self.items = [] ``` 4. Ok now lets create an instance of our house ``` winterHome = House("Winter Home", "123 1st Street", "Berwick", "PA", 18603) summerHome = House("Summer Home", "456 2nd Ave", "Virginia Beach", "VA", 23452) ``` 5. Ok so we have 2 houses now. the self.floors means that every house we create will have 2 floors..this is a set value. The self.furniture is an empty list that we will use a method to add items to the house 1. 1st lets make a way to print the house 2. Then lets go ahead and add our stuff to the house 3. Maybe a way to print our items too ``` def printHouse(self): print(f"Our {self.name} is located at {self.street} {self.city}, {self.state} {self.zip}. There are {self.floors}") def addItems(self,item): self.items.append(item) def printItems(self): print(f"Our {self.name} has the following items: {self.items}") ``` - Now we need to call these functions ``` winterHome.printHouse() summerHome.printHouse() winterHome.addItems('Couch') winterHome.addItems('Bed') summerHome.addItems('Pool') summerHome.addItems('Grill') summerHome.printItems() winterHome.printItems() ``` So there you have it. Classes with attributes and methods. # Link to Code: - [Classes](https://pythontutor.com/visualize.html#code=class%20House%3A%0A%20%20%20%20def%20__init__%28self,%20name,%20street,%20city,%20state,%20zip%29%3A%0A%20%20%20%20%20%20%20%20self.name%20%3D%20name%0A%20%20%20%20%20%20%20%20self.street%20%3D%20street%0A%20%20%20%20%20%20%20%20self.city%20%3D%20city%0A%20%20%20%20%20%20%20%20self.state%20%3D%20state%0A%20%20%20%20%20%20%20%20self.zip%20%3D%20zip%0A%20%20%20%20%20%20%20%20self.floors%20%3D%202%0A%20%20%20%20%20%20%20%20self.items%20%3D%20%5B%5D%0A%20%20%20%20%0A%20%20%20%20def%20printHouse%28self%29%3A%0A%20%20%20%20%20%20%20%20print%28f%22Our%20%7Bself.name%7D%20is%20located%20at%20%7Bself.street%7D%20%7Bself.city%7D,%20%7Bself.state%7D%20%7Bself.zip%7D.%20%20There%20are%20%7Bself.floors%7D%22%29%0A%20%20%20%20%0A%20%20%20%20def%20addItems%28self,item%29%3A%0A%20%20%20%20%20%20%20%20self.items.append%28item%29%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20def%20printItems%28self%29%3A%0A%20%20%20%20%20%20%20%20print%28f%22Our%20%7Bself.name%7D%20has%20the%20following%20items%3A%20%7Bself.items%7D%22%29%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%0A%0AwinterHome%20%3D%20House%28%22Winter%20Home%22,%20%22123%201st%20Street%22,%20%22Berwick%22,%20%22PA%22,%2018603%29%0AsummerHome%20%3D%20House%28%22Summer%20Home%22,%20%22456%202nd%20Ave%22,%20%22Virginia%20Beach%22,%20%22VA%22,%2023452%29%0A%0AwinterHome.printHouse%28%29%0AsummerHome.printHouse%28%29%0A%0AwinterHome.addItems%28'Couch'%29%0AwinterHome.addItems%28'Bed'%29%0A%0AsummerHome.addItems%28'Pool'%29%0AsummerHome.addItems%28'Grill'%29%0A%0AsummerHome.printItems%28%29%0AwinterHome.printItems%28%29&cumulative=false&heapPrimitives=nevernest&mode=edit&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    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.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully