Classes - CS50P Shorts

CS50 · Beginner ·💻 AI-Assisted Coding ·2y ago

Key Takeaways

Creates classes in Python to track packages with attributes like ID, sender, recipient, and weight

Full Transcript

[Music] well hello one and all and Welcome to our short on classes we'll see here why we should create classes and the syntax we can use to do so now I have here a program called packages. py whose goal is to really track packages we're sending between different users that we have I have here a list called packages and notice how it has two packages represented as strings inside of it I one package one Alice to Bob 10 kg and another one package two Bob to Charlie 5 kog so two different packages here inside of my list now there are a few different reasons you probably don't want to be using strings to represent these packages here now one of them is strings are flexible but arguably too flexible I could very easily change the ordering that I have for each of these pieces of information about a package here I did follow this same pattern of maybe package number colon uh sender and then recipient comma weight but I could very easily stray from that as I add more and more packages I might want something that's a little more rigid can store information in a better way so thankfully I do have at my disposal this thing called a class now a class operates a bit like a template that I can use to create various objects in my my code I could think of each of these packages as being some object in fact I could look in the real world and see a package as some object and I could think of my class as being a template to later create some object that looks a lot like a package but a particular package so here I have some pieces of information about this package I probably want to encapsulate to put inside my template uh one might be the ID of the package is it 1 2 3 some unique idea to refer to it later on I might also want to store the sender of the package as well as the recipient alongside of the weight as well so classes these templates allow me to encapsulate this information in a single place and not worry about all this different formatting that I have to maintain consistently in my code if I were to use something like a string for instance so to create a class toate this template for a package I could literally type class just like this followed by the name for my class in this case package and by uh by uh convention these class names tend to begin with uppercase so here we have capital P package and I could later on fill in this package but here I'm getting started on creating this template for some package that I can then use later on to create particular packages so let's think about what a template for a package might ideally store well to do so I need to First create this particular method one called Dunder in it which allows me to use my template my class here to later on instantiate or create some particular object from this template from this class so here I'll Define in this case Dunder in it Dunder stands for these double underscores here and it turns out that this by convention must take at least one argument one called self when I create later on a new object this self refers to the instance that new object I'm creating when I were for inst to call something like package we'll see that later on the other thing this really wants to probably take as input here is all of the pieces of information I need to create some new package what we said down below we want to encapsulate or really combine a few different pieces of information one is the package ID d one is the uh sender and one is the recipient and one is the weight of the package here so I could go ahead and say as input uh to this particular template that I have here creating some new object later on I'll go ahead and say each package has a number or some ID it also has a sender a recipient and in this case a weight and this is my Dunder init method allows me in this case to specify what information I'm encapsulating as part of this template in this case all packages have some number some sender some recipient and some weight well now we might see where self comes in within this Dunder init method I could actually go ahead and try to assign to this new instance of my object everything I've passed in down below I could say self. number equals number self. sender equals sender self. recipient equals recipient and self. we equals weight now this might be a little bit confusing at first especially seeing self and especially having made up some of these actual pieces of information down below but let's see what it could do for us and maybe walk through what's happening each step along the way so I ideally I want to rewrite my code online 10 here this is not what I should be doing to represent packages but I do now have this template that I'm able to use to instantiate to create various objects in this case individual packages well down below here let's try doing just that I'll remove these strings and I'll then go ahead and as input to my list well I'll create some new package and I can do so in this case by typing my class name package followed by parentheses and now here is where I can actually use those arguments I gave to Dunder in in it when I use package parentheses I'm actually at least on one step of the way calling Dunder in it to create for myself some new instance of this template we said here that this should take Beyond self uh number sender recipient and weight as arguments so I can do this I could say my first package's number is one and it's the sender the sender is Alice uh the recipient in this case is Bob and and the weight the weight in this case will be 10 and so this is the way we've now defined some new instance of this particular class a given package that has a number one the sender Alice the recipient Bob and the weight 10 for 10 kg let's go ahead and add in another package another instance of this class I could type package again and I'll say number equals 2 sender equals Bob uh maybe recipient here was Charlie as before and the weight the weight of this package was five and so this is this is here our way of representing that second package in our list and now we have a much more methodical way of storing or encapsulating this information thanks to the class we've created above but now let's see what this was doing here we said self. number self. sender self. recipient self. weight we later on we'll actually see if we can access these things called instance variables but we'll see that in another short this then was our short on classes how to create them and the syntax to do so we'll see you next time

Original Description

*** This is CS50, Harvard University's introduction to the intellectual enterprises of computer science and the art of programming. *** HOW TO SUBSCRIBE http://www.youtube.com/subscription_center?add_user=cs50tv HOW TO TAKE CS50 edX: https://cs50.edx.org/ Harvard Extension School: https://cs50.harvard.edu/extension Harvard Summer School: https://cs50.harvard.edu/summer OpenCourseWare: https://cs50.harvard.edu/x HOW TO JOIN CS50 COMMUNITIES Discord: https://discord.gg/cs50 Ed: https://cs50.edx.org/ed Facebook Group: https://www.facebook.com/groups/cs50/ Faceboook Page: https://www.facebook.com/cs50/ GitHub: https://github.com/cs50 Gitter: https://gitter.im/cs50/x Instagram: https://instagram.com/cs50 LinkedIn Group: https://www.linkedin.com/groups/7437240/ LinkedIn Page: https://www.linkedin.com/school/cs50/ Medium: https://cs50.medium.com/ Quora: https://www.quora.com/topic/CS50 Reddit: https://www.reddit.com/r/cs50/ Slack: https://cs50.edx.org/slack Snapchat: https://www.snapchat.com/add/cs50 SoundCloud: https://soundcloud.com/cs50 Stack Exchange: https://cs50.stackexchange.com/ Telegram: https://t.me/cs50x Threads: https://www.threads.net/@cs50 TikTok: https://www.tiktok.com/@cs50 Twitter: https://twitter.com/cs50 Twitter Community: https://twitter.com/i/communities/1722308663522594923 YouTube: http://www.youtube.com/cs50 HOW TO FOLLOW DAVID J. MALAN Facebook: https://www.facebook.com/dmalan GitHub: https://github.com/dmalan Instagram: https://www.instagram.com/davidjmalan/ LinkedIn: https://www.linkedin.com/in/malan/ Quora: https://www.quora.com/profile/David-J-Malan Threads: https://www.threads.net/@davidjmalan TikTok: https://www.tiktok.com/@davidjmalan Twitter: https://twitter.com/davidjmalan *** CS50 SHOP https://cs50.harvardshop.com/ *** LICENSE CC BY-NC-SA 4.0 Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License https://creativecommons.org/licenses/by-nc-sa/4.0/ David J. Malan https://cs.harvard.edu/malan m
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from CS50 · CS50 · 0 of 60

← Previous Next →
1 Hello, World: Hadi Partovi
Hello, World: Hadi Partovi
CS50
2 Content Distribution and Archival in a Digital Age
Content Distribution and Archival in a Digital Age
CS50
3 CS50 2014 - Week 1
CS50 2014 - Week 1
CS50
4 CS50 2014 - Week 3
CS50 2014 - Week 3
CS50
5 CS50 2014 - Week 0, continued
CS50 2014 - Week 0, continued
CS50
6 CS50 2014 - Week 4
CS50 2014 - Week 4
CS50
7 Week 3, continued
Week 3, continued
CS50
8 Quiz 0 Review
Quiz 0 Review
CS50
9 CS50 2014 - Week 3, continued
CS50 2014 - Week 3, continued
CS50
10 CS50 2014 - Week 7
CS50 2014 - Week 7
CS50
11 CS50 2014 - Week 7, continued
CS50 2014 - Week 7, continued
CS50
12 Breaking Through The (Google) Glass Ceiling by Christopher Bartholomew
Breaking Through The (Google) Glass Ceiling by Christopher Bartholomew
CS50
13 Introduction to Amazon Web Services by Leo Zhadanovsky
Introduction to Amazon Web Services by Leo Zhadanovsky
CS50
14 CS50 2014 - Week 9
CS50 2014 - Week 9
CS50
15 How to Build Innovative Technologies by Abby Fichtner
How to Build Innovative Technologies by Abby Fichtner
CS50
16 Light Your World (with Hue Bulbs) by Dan Bradley
Light Your World (with Hue Bulbs) by Dan Bradley
CS50
17 Building Dynamic Web Apps with Laravel by Eric Ouyang
Building Dynamic Web Apps with Laravel by Eric Ouyang
CS50
18 CS50 2014 - CS50 Lecture by Steve Ballmer
CS50 2014 - CS50 Lecture by Steve Ballmer
CS50
19 CS50 2014 - Week 10
CS50 2014 - Week 10
CS50
20 This is CS50 with Steve Ballmer?
This is CS50 with Steve Ballmer?
CS50
21 Meteor: a better way to build apps by Roger Zurawicki
Meteor: a better way to build apps by Roger Zurawicki
CS50
22 Data Analysis in R by Dustin Tran
Data Analysis in R by Dustin Tran
CS50
23 Data Visualization and D3 by David Chouinard
Data Visualization and D3 by David Chouinard
CS50
24 CS50 2014 - Week 6
CS50 2014 - Week 6
CS50
25 Build Tomorrow's Library by Jeffrey Licht
Build Tomorrow's Library by Jeffrey Licht
CS50
26 CS50 2014 - Week 9, continued
CS50 2014 - Week 9, continued
CS50
27 Essential Scale-Out Computing by James Cuff
Essential Scale-Out Computing by James Cuff
CS50
28 iOS App Development with Swift by Dan Armendariz
iOS App Development with Swift by Dan Armendariz
CS50
29 Sam Clark Leads Yale Students on Tour to CS50 at Harvard
Sam Clark Leads Yale Students on Tour to CS50 at Harvard
CS50
30 3D Modeling and Manufacture by Ansel Duff
3D Modeling and Manufacture by Ansel Duff
CS50
31 CS50 2014 - Week 5, continued
CS50 2014 - Week 5, continued
CS50
32 hello, world
hello, world
CS50
33 CS50 2014 - Deep Thoughts - Hash Table
CS50 2014 - Deep Thoughts - Hash Table
CS50
34 CS50 2014 - Deep Thoughts - Binary Tree
CS50 2014 - Deep Thoughts - Binary Tree
CS50
35 CS50 2014 - Deep Thoughts - Scratch
CS50 2014 - Deep Thoughts - Scratch
CS50
36 CS50 2014 - Deep Thoughts - MySQL
CS50 2014 - Deep Thoughts - MySQL
CS50
37 LaunchCode Visits CS50
LaunchCode Visits CS50
CS50
38 CS50 Live, Episode 100
CS50 Live, Episode 100
CS50
39 CS50 Field Trip to Google
CS50 Field Trip to Google
CS50
40 This is CS50 AP
This is CS50 AP
CS50
41 Week 4: Monday - CS50 2011 - Harvard University
Week 4: Monday - CS50 2011 - Harvard University
CS50
42 Week 2: Wednesday - CS50 2011 - Harvard University
Week 2: Wednesday - CS50 2011 - Harvard University
CS50
43 Week 1: Wednesday - CS50 2011 - Harvard University
Week 1: Wednesday - CS50 2011 - Harvard University
CS50
44 Week 11: Monday - CS50 2011 - Harvard University
Week 11: Monday - CS50 2011 - Harvard University
CS50
45 Week 3: Wednesday - CS50 2011 - Harvard University
Week 3: Wednesday - CS50 2011 - Harvard University
CS50
46 Week 12: Monday - CS50 2011 - Harvard University
Week 12: Monday - CS50 2011 - Harvard University
CS50
47 Week 1: Friday - CS50 2011 - Harvard University
Week 1: Friday - CS50 2011 - Harvard University
CS50
48 Week 3: Monday - CS50 2011 - Harvard University
Week 3: Monday - CS50 2011 - Harvard University
CS50
49 Week 10: Wednesday - CS50 2011 - Harvard University
Week 10: Wednesday - CS50 2011 - Harvard University
CS50
50 Week 2: Monday - CS50 2011 - Harvard University
Week 2: Monday - CS50 2011 - Harvard University
CS50
51 Week 9: Monday - CS50 2011 - Harvard University
Week 9: Monday - CS50 2011 - Harvard University
CS50
52 Week 7: Monday - CS50 2011 - Harvard University
Week 7: Monday - CS50 2011 - Harvard University
CS50
53 Week 5: Monday - CS50 2011 - Harvard University
Week 5: Monday - CS50 2011 - Harvard University
CS50
54 Week 5: Wednesday - CS50 2011 - Harvard University
Week 5: Wednesday - CS50 2011 - Harvard University
CS50
55 Week 7: Wednesday - CS50 2011 - Harvard University
Week 7: Wednesday - CS50 2011 - Harvard University
CS50
56 Week 8: Monday - CS50 2011 - Harvard University
Week 8: Monday - CS50 2011 - Harvard University
CS50
57 Week 9: Wednesday - CS50 2011 - Harvard University
Week 9: Wednesday - CS50 2011 - Harvard University
CS50
58 Week 8: Wednesday - CS50 2011 - Harvard University
Week 8: Wednesday - CS50 2011 - Harvard University
CS50
59 Week 10: Monday - CS50 2011 - Harvard University
Week 10: Monday - CS50 2011 - Harvard University
CS50
60 Week 2: Wednesday - CS50 2010 - Harvard University
Week 2: Wednesday - CS50 2010 - Harvard University
CS50

Related Reads

📰
Beyond Replication: Theo Browne on AI's 'Skeuomorphic Phase' and the Future of Development
Learn about AI's 'Skeuomorphic Phase' and its implications on the future of development with Theo Browne's insights
Dev.to AI
📰
Rust and Artificial Intelligence: The Rust Foundation's Position
Learn about the Rust Foundation's stance on Rust and Artificial Intelligence and its potential applications
Hacker News
📰
Struggling with manual coding despite understanding basics – AI vs. Traditional Path?
Learn to leverage AI tools for coding and focus on high-level problem-solving to become a successful problem-led entrepreneur
Reddit r/learnprogramming
📰
10 Python AI Automation Scripts for Devs
Boost productivity with 10 Python AI automation scripts, including image classification and NLP tasks, to streamline dev workflows
Dev.to AI
Up next
Free WooCommerce Product Carousel Using Elementor Angie AI | Product Marquee Widget
Quick Tips - Web Desiign & Ai Tools
Watch →