Python Classes and Objects || Python Tutorial || Learn Python Programming
Video Title: Python Classes and Objects || Python Tutorial || Learn Python Programming
Classes are a fundamental tool in any respectable programming language. Think of a class as a template for creating objects with related data and functions that do interesting things with that data. Python makes it easy to create and use classes. In fact, most other programming
Languages are quite jealous of Python for its elegance and simplicity. Today, we will show you how to use classes in Python, and we will do so in a classy way. Suppose we are building a new social network called friendface, and our goal
Is to gather as much personal information as possible about our users… for reasons. We will have a lot of data about each user, and will write many functions using that data. To organize this effort, let’s start by making a
Class called user. In Python, you define a class by first writing the class keyword, followed by the name of the class. It’s recommended that you capitalize all the words in the name of your class. Finally, type a colon and press Enter. For our
First example, we will make the simplest class possible. We will do absolutely nothing. Type pass and press Enter. Pass is a way to type a line that does nothing. This is necessary, because when defining a class
You need at least one line. Now that we have defined our class, we can use it to create different users. To make a user, type in the name of the class, followed by parentheses. This looks like you are calling a method…and you are, in a fashion.
We call user 1 an instance of the user class. You can also call user 1 an object. To attach data to this object, you first type the name of the object, a dot, the name of the variable, and then give it a value. Let’s give user 1 a first name and
A last name. Since first_name and last_name are attached to an object, we call them fields. They store data specific to user one. To see that the data is in the user one object, let’s print them. You access the data the same
Way you assigned it. Type the name of the object, dot, then the name of the field. Let’s also print the last name. Excellent. By the way, you should not capitalize the name of fields, and if you use more than one word in the field name, separate the words by underscores.
This is a tradition in Python, so to avoid uncomfortable stares from your peers, I would recommend using this convention. Here the name Dave Bowman is attached to the object user 1 To make this clear, let’s create standalone variables also called first_name and last_name. These values are not attached
To a user object. If we print these values, we get the name Arthur Clarke but if you print the values attached to user 1, you get Dave Bowman. Even though we use the same variable names the values are kept separate. With classes, there is no
Limit to the number of objects you can make. Let’s make a second user called, creatively, user 2. We will give this user the name Frank Poole. We use the exact same field names as before, but this time the values are attached to user 2. To see
That Python keeps these three names separate we will print the three names. So classes are used to make objects, and each object can have different values for the same variable names. You can attach additional fields to the objects,
And they do not have to be strings. Let’s suppose user 1 has an age of 37, an integer. And for user 2, let’s assign a value for his favorite book. We are now in a situation where user 1 and user to have different fields attached to them.
The user 1 has an age. User 2 does not. User 2 has a favorite book, but user 1 does not. If you print the age for user 1, you can see the value is there. And if you look at the type for age ,it is indeed an integer. Now look what happens
When we try to print the age for user 2, which we have not assigned. We get an attribute error so please exercise caution. If you are not certain an object has a specific field, you may experience an error. I know what you are thinking.
Why did we go through the trouble of creating a class, when we could have just as easily stored the data in a dictionary? We will now see the additional features of classes which allow them to transcend a simple dictionary. By adding methods using object initialization, including
Help text, we can turn our simple class into a data powerhouse. The first feature we will add is an init method. A function inside a class is called a method. init is short for initialization and some languages call initialization methods constructors. The name of this method is init with double underscores before and
After the method. This method is called every time you create a new instance of the class. The first argument to this method is the word self, which is a reference to the new object that is being created. You can add additional
Arguments after self. We’ll add full name and birthday arguments. The first thing we will do is store these values to fields in the object. We do this by typing self, dot, the field name, and then assign it a value. We’ll store the full name
As a field called name, but we will store the value birthday in a field also called birthday. Be careful here. This birthday is the value provided when you create a user object, but THIS birthday is the field that stores the value. To
Keep this example simple, we will assume the birthday is in year-month-day format. We will now create a user and use the init method. Like before, you type in the name of the class and parentheses. This time, we must provide two values,
Because the anit method is expecting two values: first, the name, then the birthday. You can test that these values are stored in the object by printing them. The data is all there. Let’s add another feature to our class. In the init method,
Let’s break apart the name and extract the first and last names. We will call the split method on the full name, and pass in a space. This will chop the name into pieces by cutting whenever it encounters a space. The pieces will be
Stored in an array. The first name will be the first string in the array, and the last name will be the last string in the array, which we can access using index minus 1. Notice that we attach the first name to self
But didn’t do this with the last name. We’ll see what happens as a result. Let’s create the user just as before, and then print the full name. first name last name and birthday. Run. We get an attribute error when we try to print the last name.
This is because we didn’t attach last name to the object using self. What happened is we assigned the value to the variable last_name ,which only exists until the end of the method. This is a quick fix. Let’s go back and attach
Last_name to self. Now, if you run the code everything works as expected. We can further improve this class by adding some help text. To do this, you type a special string which we call a docstring. This is a string inside triple quotes that you type right after the first line.
Now look what happens when you call the help function for this class. Python displays a useful overview of the user class. It displays the docstring as a summary, and it also shows the arguments that are expected in the init method. Even if you are writing code only for yourself,
It is a good habit to write a docstring. You may have to use a class years after you wrote it. A few seconds of typing is worth hours of sanity down the line. Also notice that the help call displays two additional
Items: dict and weekref. We will discuss these in the next video on classes. Let’s add another method to the user class that will return the age of the user in years. Like the init method, the first argument is self. And to showcase our responsible nature, add a brief docstring.
We will compute the age using the user’s birthday, so this method does not require any additional parameters. Since we will be working with states we need to import the date/time module. Let’s first get today’s date. We shall assume it is May 12th, 2001 so that everyone who
Tests this code will get the same answer. Next, let’s convert the birthday string into a date object. There is a way to do this in a single line, but for clarity we will use a more direct method. In this example, we assume the birthday was a
String in year-month-day format. From this string, we can extract the year, the month, and the day as integers. With these three integers ,we can create a date object for the users birthday. If you compute the difference between today
And the birthday, you get a time delta object. The time delta object has a field called days. Ignoring leap years, we can now compute the age in years by dividing by 365. Finally, return the age as an integer. To test this method, create the
User once more. Then call the age method, and print the result. 30 years old. Notice that you did not type self when calling the age method. The self keyword is only used when writing a method. By the way, if you call the help function once more you
Will see the summary now includes a description of the age method. Classes: powerful contraptions. They empower you to group together data (which we call fields) and related functions (which we call methods) into a kind of factory for creating many objects (which we call instances).
Socratica is somewhat like a class which creates instances of high quality videos. Who sees our videos is determined by a mysterious black box called the YouTube Algorithm. The best way to tell the YouTube Algorithm that our videos are worthy of watches is to engage. Tell someone on friendface our video exists
And that it’s in a class by itself.
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Wear a Socratica Python shirt for good luck coding: https://shop.socratica.com/products/python-by-socratica
This channel is a great one on Python. There is one channel "techie talkee" which explains all important features and concepts in Python – Data Science ,SciPy , Matplotlib, Pandas very clearly with excellent examples. It will be very useful to watch.
I learned python with this channel! Great channel!
Wow, this is fantastic and beats any python learning on youtube. I love this stuff.
I think I saw Wednesday aadams series right now btw you really nailed it !!! Amazing explanation!!! Really loved it the way you explained!!!! ✨
@0:47 Did u just call matrix as a documentary cus in the Ai teacher's perspective its based on the reality and not fiction? is it intentional or im just too creative
This was legitimately great.
The humor alone 🤣🤣🤣
"Today we will show you how to use classes in python, and we will do so in a classy way…"😂😂🤞
Second video on classes? 🙏 Gotta resolve the _weakref_ cliffhanger!
thank you l really understood everything quickly ,bless you Robot Woman
Thank you so much. I am big fan of Socratica channel. Looking for Python lessons I found you in Youtube.It is clear and informative.
She is 🐍 Python.
This is trash
Amazing explain
I wish my Python programming classes were this interesting and engaging.
Well done.
😂 great content IMO really enjoyed watching this.
Love this!😆
… and it's in a class by itself. Truly!
Genial, gracias.
Awesome video… so creative
I just randomly found this channel, and this is the coolest, most creative way of teaching I have ever seen. It makes me feel like I am some kind of commander, and I am being briefed an important message by an AI named Socratica. Thank you, Socratica, for making these videos.
Cool stuff! I love this.
Yo this is awesome, I learned a lot thanks.
Thanks
Nice videos. I like them all. Thanks for creating these to easy to understand tutorials.
This is the best 10 minutes in my life
I understand… At least I pretend to while nodding.
This was a really good watch. Thank you for your work!
Is she the same lady giving tesk to the IGI project game main character
0:37 "and our goal is to gather as much personal information possible about our users ……. for reasons"
lololol
I was expecting HAL9000 will be eventually mentioned, but didn't know it's gonna be in such clever way.
9:20 I find the „self“ argument and the syntax as well hard to understand. When is best to be used, when is best not to be used?
Is there a good video about the self arguement?
Doooooooooooope 💯
Truly an excellent video! Your work is of the highest quality and thank you very much.
Amazing, Loved it, the way you explain
I have been watching your videos for quite sometimes, the way you teach the concepts of python is like you are standing somewhere outside this world, May be on another planet. 😀😂
Sounds like talking via dark web.
Bro, is nobody going to talk about the fact that she sounds like GladOs from Portal ?
I'm hooked. You had me on the SQL video's. I was laughing and then learning. Now it's Python. Thanks
After seeing a lot of videos on python, objects n classes I wasn’t getting all my answers.. every video wasn’t satiating my doubts . Now man after watching this I really feel this is it.. ultimate vdo, most perfect n crystal clear explanation.No extra talk. So happy. It’s an art of teaching.
THANK YOU FOR THIS!!
When I run this at the end I get a type error.. " yyyy = str(self.birthday[0:4])
TypeError: 'int' object is not subscriptable"
THIS👏IS👏GOOD👏QUALITY👏CONTENT👏
youre amazing, clear, concise, comprehensive
What you said at last is very true 🙂 thanks. liked the humor
Feels like an gpt chat created an AI for giving a clases about Claesses.
I love these videos and the dry humor included.
I feel like these videos are from future 😂😂😂😂
Beyond the excellent content and creative, very well-done presentation, I also love the sense of humor. FriendFace has always been one of my favorite in-joke references in the IT world… or… Crowd. lol.