Instance Variables - CS50P Shorts
Key Takeaways
Explains instance variables in Python
Full Transcript
Well, hello one and all and welcome to our short on instance variables. In a prior short on defining classes, we saw a way to create a class or a template for a package. One that we called in fact package and down below we created several instances of that class, of that template to represent individual packages here. So, notice on lines 11 and 12, I have here some code that defines for me two instances of this class that we called package. It's a bit like me creating a template for something called a package and down below creating two particular packages. Now, I did so as follows. I said down below that this first package has a number one, the sender was Alice, the recipient was Bob, and the weight here was 10. But, what actually happens as we call like a some code on line 11? Well, we said we'd come back to these lines three through six here. So, let's take a look at exactly what's happening. Well, when I run some line of code like on line 11, I again am creating some new instance of this class I've defined called package and along the way, thanks to Python, this method called dunder init is called. And we said here that it takes as input self, which refers to the new instance of the class that we are creating. And it seems like what I'm doing here is taking in various inputs like number, sender, recipient, and weight and assigning them to various attributes or properties of this class, of this instance of the class here. So, it turns out that each of these things, self.number, self.sender, and so on, are called instance variables. They're variables that belong to some instance of this class and they are defined for me whenever I call package down below. So, let's look at number for instance. Number equals one. Well, I'm now passing in to dunder init this argument number with the value one. And what happens to that number one? Well, it actually gets assigned as an instance variable within this instance of the class. So, this instance here has a variable called number. And so on for sender, recipient, and weight. But, let's see this in action here. I'll come down below. And let me try to actually access these instance variables that I claim I set up on lines three through six. Well, I could imagine, let's say, looping through every package that I have inside of this list of packages. Maybe printing them out along the way. Well, to do so, I could use a for loop. I want to print for each package I have. So, I'll say for package in the list of packages and then I'll throw this in a little later. Notice first that package will first refer to the package on line 11 and then on the next iteration the package on line 12. So, very simple here. Let's start off by printing something. We could print the package as a whole, but I don't think we'd get a very good output. What I could do instead is try printing, in this case, something like the instance variable that we called number just to start with. package.number So, if I want to access an instance variable, I can in this case refer to the individual instance of the class, package here, followed by dot, followed by the instance variable's name, just like we said up above. In this case, number. So, if I try this, I'll try python of packages.py, hit enter, we'll see one and two. But, just to hammer this home, let me try instead one and three and now I'll hopefully see one and three. So, it does seem as I defined in dunder init that the input to these variables various parameters of packages arguments here, they actually get assigned as instance variables of each instance of my class. Now, let's try accessing more and maybe printing these packages in a in a more in a prettier way. So, here I can do this. I can go back to the format I had before, but now use a Python f-string in the packages instance variables. I could say package and then package.number just like this. If I were to run this, I could see package one and package two. Pretty good. What if I tried maybe the same format from before? I could try colon and then I could enter in, let's say, the package's sender uh to package.recipient. And let's see what we see here. python packages.py package one, Alice to Bob, package two, Bob to Charlie. This is all happening by accessing these instance variables that I set up in dunder init as well. Let's now add in the weight. I'll say comma the weight was, in this case, in kilograms. I'll go ahead and now um try to run this again. I'll go ahead and go back to my terminal. python packages.py and we'll see a much prettier way of formatting these individual packages. In fact, we can get the benefits of the strings we saw, let's say, in the short on defining classes, which allows them to be more prettily printed, but we also get the benefits of this class here. So, we're encapsulating information now using these things called instance variables. This then was our short on instance variables and 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Hello, World: Hadi Partovi
CS50
Content Distribution and Archival in a Digital Age
CS50
CS50 2014 - Week 1
CS50
CS50 2014 - Week 3
CS50
CS50 2014 - Week 0, continued
CS50
CS50 2014 - Week 4
CS50
Week 3, continued
CS50
Quiz 0 Review
CS50
CS50 2014 - Week 3, continued
CS50
CS50 2014 - Week 7
CS50
CS50 2014 - Week 7, continued
CS50
Breaking Through The (Google) Glass Ceiling by Christopher Bartholomew
CS50
Introduction to Amazon Web Services by Leo Zhadanovsky
CS50
CS50 2014 - Week 9
CS50
How to Build Innovative Technologies by Abby Fichtner
CS50
Light Your World (with Hue Bulbs) by Dan Bradley
CS50
Building Dynamic Web Apps with Laravel by Eric Ouyang
CS50
CS50 2014 - CS50 Lecture by Steve Ballmer
CS50
CS50 2014 - Week 10
CS50
This is CS50 with Steve Ballmer?
CS50
Meteor: a better way to build apps by Roger Zurawicki
CS50
Data Analysis in R by Dustin Tran
CS50
Data Visualization and D3 by David Chouinard
CS50
CS50 2014 - Week 6
CS50
Build Tomorrow's Library by Jeffrey Licht
CS50
CS50 2014 - Week 9, continued
CS50
Essential Scale-Out Computing by James Cuff
CS50
iOS App Development with Swift by Dan Armendariz
CS50
Sam Clark Leads Yale Students on Tour to CS50 at Harvard
CS50
3D Modeling and Manufacture by Ansel Duff
CS50
CS50 2014 - Week 5, continued
CS50
hello, world
CS50
CS50 2014 - Deep Thoughts - Hash Table
CS50
CS50 2014 - Deep Thoughts - Binary Tree
CS50
CS50 2014 - Deep Thoughts - Scratch
CS50
CS50 2014 - Deep Thoughts - MySQL
CS50
LaunchCode Visits CS50
CS50
CS50 Live, Episode 100
CS50
CS50 Field Trip to Google
CS50
This is CS50 AP
CS50
Week 4: Monday - CS50 2011 - Harvard University
CS50
Week 2: Wednesday - CS50 2011 - Harvard University
CS50
Week 1: Wednesday - CS50 2011 - Harvard University
CS50
Week 11: Monday - CS50 2011 - Harvard University
CS50
Week 3: Wednesday - CS50 2011 - Harvard University
CS50
Week 12: Monday - CS50 2011 - Harvard University
CS50
Week 1: Friday - CS50 2011 - Harvard University
CS50
Week 3: Monday - CS50 2011 - Harvard University
CS50
Week 10: Wednesday - CS50 2011 - Harvard University
CS50
Week 2: Monday - CS50 2011 - Harvard University
CS50
Week 9: Monday - CS50 2011 - Harvard University
CS50
Week 7: Monday - CS50 2011 - Harvard University
CS50
Week 5: Monday - CS50 2011 - Harvard University
CS50
Week 5: Wednesday - CS50 2011 - Harvard University
CS50
Week 7: Wednesday - CS50 2011 - Harvard University
CS50
Week 8: Monday - CS50 2011 - Harvard University
CS50
Week 9: Wednesday - CS50 2011 - Harvard University
CS50
Week 8: Wednesday - CS50 2011 - Harvard University
CS50
Week 10: Monday - CS50 2011 - Harvard University
CS50
Week 2: Wednesday - CS50 2010 - Harvard University
CS50
Related Reads
📰
📰
📰
📰
If AI Writes Your Code, Why Learn Python?
Medium · AI
Stop Hallucinating Terraform: Building a Private Internal Developer Portal with RAG and Gemma 2
Medium · AI
The Poor Guy* Guide to Fable-level Vibe Coding for $80/month
Medium · Programming
The Poor Guy* Guide to Fable-level Vibe Coding for $80/month
Medium · LLM
🎓
Tutor Explanation
DeepCamp AI