Object Oriented Programming Introduction - Intermediate Python Programming p.13

sentdex · Beginner ·🛠️ AI Tools & Apps ·9y ago

Key Takeaways

Introduces Object Oriented Programming concepts in intermediate Python programming

Full Transcript

what is going on everybody and welcome to part 13 of our intermediate Python programming tutorial series in this tutorial what we're going to be doing is jumping into object-oriented programming so objects are any programming is a concept that's a little hard to define in very few words but I think it's a lot easier to understand when you just do it but the purpose of object-oriented programming is to create objects and these objects have attributes and then they also have methods so the attributes are like characteristics and the methods are basically actions or ways for us that we can actually interact with these objects now for this tutorial I'm going to be making use of Pi game which is a pretty simple game library for Python it works really well at least on Windows and Linux on Macintosh it's a little more difficult but you can go to PI game dot org slash wiki slash Macintosh and they have like updated instructions at least it looks here like media will work on the latest Python 3 as far as I know it definitely works on 3.1 so you might have to get a different version of Python that might be kind of annoying but I do think that PI game or some other way of actually visually seeing what you're working with is an exceptional way to learn either just programming in general or some sort of programming concept especially object-oriented programming where we're creating this thing that has like characteristics and does things and is interacted with and objects can interact with each other and all this kind of stuff I think it makes a lot of sense to use something like PI games so we can actually visualize what's happening as well so that's what we're going to do on Windows and Linux you should just be able to do pip install PI game and you're good to go so hopefully you can figure that out if you have questions feel free to ask in the in the comments and otherwise let's get started so first of all the thing that creates an object is a class so we're going to define we'll just start by defining a class and we're going to call this class blob and what we're going to do at least to start is we're going to be creating a sort of blob world that consists of an environment that these blobs are living within and the blobs will interact with each other but also just the general environment that can do things in the environment but first we need to define what this blob object actually is what are its attributes and what are things that this blob can actually do so just class blob : and you're good to go and right away we're going to define the first our first method and what we're going to do is we're going to define the dunder itter or not send Ritter ah dunder init method donor hitter will come eventually but dunder init is the person we're going to do and we're going to pass self and then we're going to pass color so what these arguments do is self is just this this instance it's this object instance that we're going to be able to share throughout the entire object here so throughout the entire class the things that we define is like self dot something like to start we're going to have maybe self dot color since we're passing color here that's going to have some sort of value we're going to set that to be whatever color is so now this object this is an attribute of this object right and we can reference it in the definition of the class anywhere and in another method it doesn't have to be in the init method so we can reference this elsewhere using self dot but then outside of this when we actually do create the object and save it to a variable name we do that variable named color and that is how we can access the color attribute you'll see that more as when we get there but self is just you can pick any name for self but self is just the standard kind of protocol that people use since we're trying to do as proper as possible code code that scales code that can be read by other people we're going to call that self and we're not going to deviate from that I'm not sure I've ever seen anyone deviate from that but you could if you wanted to be a real jerk so anyway there we have that the other things that we're going to add to this blob basically the the init method what this is going to do is when the when the object is created whatever the init method is is just going to run okay so when when the blob is born these are things that are going to be set about the blob so we're saying when this blob is born it's assigned a color because we've got color as a as an argument here we're also going to assign it a location because it's going to be born and it's going to appear in this environment somewhere so we're going to say self dot X and we're going to say this we have an imported random but we'll do that in here in a second random dot R and range and we'll say it's zero to the width and we also don't have width yet we're going to we'll bring that one in as well but I just wanted to keep this just the class for now but the width will be the the width of the the environment basically so in pygame you'll bring up a window we'll probably do like 800 by 600 so width will be like 800 pixels so this could be anywhere between 0 and 800 then we're going to do set loops then we'll do self dot y equals random dot R and range and this will be 0 to the height now we're going to do a self dot size so how big is the blob going to be basically these are just all things that we have to know about the blob to show the blob on the screen right we need to know at least these things that's why they're going to be in the initialization method here so self that size we're going to random dot R and range and this will be 4 to 8 it's a little arbitrary seeming at the moment but but pygame does sizes this way and it isn't maybe it's pixels it's not pixels I don't think it's not 4 to 8 so I'm not really sure but obviously the bigger you go we can kind of tweak that and you'll see what I mean it it's the same thing that's like maybe I don't know I'm not really sure what the what the metric is here but anyway I don't think it's pixels uh and then that's I think that's it that's all we need nd dunder init method now now we're going to say another method and there are other basically the init method again this is one of these special methods of Python they're also lovingly referred to as magic methods and there are a lot of magic methods we will talk about a few others I accidently Freudian slip the dunder itter method which we can use with generators for example and a lot of other cool things which I'm not really sure blob will have added under it err method but maybe who knows but anyways there are a lot of magic methods but you don't have to have them and similarly you don't have to have an init method you're well you have one you just don't have to define one so you didn't have to do this but in many cases you're going to need you're going to want to set some initial attributes so so we have one but we can also define other methods and methods pretty much look and act just like functions okay they just need self to be passed so this next method we're going to call move move and just for the record naming conventions and such methods have the same naming convention as functions classes have Studley cased names so this might be blob class or or something like that but we're just going to keep it blob one word so now the move method we don't we're not going to pass anything besides self here and we're just going to say self dot move x4 now is going to be equal to random dot R and range will do negative one to two then we're going to say self dot move Y will be equal to random dot R and range negative one to two again so this will be basically anything from negative one it can be any possibility negative one zero or one so in these movements these movements will be in pixel so at each frame it can either move it can move one pixel in any direction up to one anyways sometimes it might not then we're going to say we're going to update self dot X so this is how we're kind of sharing we're not redefining necessarily what we are redefining but basically this is going to modify the self dot X attribute and we're gonna say plus equals self dot move X and then self dot y we're going to say plus equals self don't move Y now the idea is that this is going to interact in an environment and for now we're going to set some limitations on the blob class and we're just going to say that we know it's interacting with in this window of width and height so we're going to go ahead and just say basically if self dot X is less than zero we're going to say self dot x equals zero l if self dot X is greater than the width we're going to say self dot x equals so x equals the width and then we're gonna do the same thing basically with y so we're gonna say if self lips we're going to say if self dot y is less than zero self dot y equals zero and then LF self dot y is greater than the height we're going to say self dot y equals height cool so where do that alternatively one thing you can do just because this takes up a lot of space you can do this and put those on one line I'll let you decide which you think is better personally I find this to be cleaner if you've got really simple simple information I think it's easier to read personally so if you're not really having too much on each one I don't see a problem with this but if I was to guess I would say that's not Pepe but I'm going to keep it on one line I preferred on one line I think it's more beautiful so we have ourselves are starting blob class and I think what I'm going to do is stop it here that's all we're going to add to the blob class for now and in the next tutorial we are going to use PI game we're going to create the environment and we're going to plop this blob into the environment and see what we got so if you have questions comments concerns up to this point feel free to leave them below otherwise I'll see you in the next tutorial

Original Description

Welcome to part 13 of the intermediate Python programming tutorial series. In this tutorial, we're going to introduce the concept of Object Oriented Programming (OOP), which is a topic that will be present in quite a bit of the rest of this entire series. Almost immediately, you will be able to see some of the benefits of OOP, but OOP is also a gift that keeps on giving. Object Oriented Programming is a hard thing to define, but it's centered around the creation of objects and interacting with them, as you might guess. Objects have characteristics and features, known as attributes, and can do various things, through their methods. The biggest feature of OOP is how well objects can be interacted with, and even molded in the future, which makes them very friendly to developers, scale, change over time, testing, and much more. https://pythonprogramming.net https://twitter.com/sentdex https://www.facebook.com/pythonprogramming.net/ https://plus.google.com/+sentdex
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →