Object Modularity Thoughts - Intermediate Python Programming p.16

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

Key Takeaways

The video discusses object modularity in intermediate Python programming, covering topics such as class design, initialization, and modularity, with tools like PGame and Python, and techniques like object-oriented programming and modular code design.

Full Transcript

hello everybody and welcome to part 16 of our intermediate Python tutorial Series in this tutorial we're going to continue talking about objectoriented programming and specifically the class that we have so far so remember those main kind of attributes and Hallmarks of what I classified as intermediate uh python one of them was modularity um the other one was like readability and scale so with readability I mean do we can we like read this and see like is this going to work and stuff like that yeah I think so uh does it scale there's really nothing in here that's crazy uh the only thing might be these lines but we'll we will talk about those lines shortly um but the other thing is this modularity right so with modularity can someone easily whether it's someone else or us take this code and quickly make use of this code well maybe so with blob like one thing that's useful to do is to take the blob class or whatever class you're writing and put it in another file and import it and then see what you think so right now it's all in this one script it's easy uh and things are working it's it seems to be working just fine we're getting some nice benefits of object-oriented programming but what I want us to do now is to take it and put it somewhere so I'm going to give it a new uh in fact let's just uh I'll just copy this copy paste and we're going to call this um I'm trying to think what a good name to call it I don't want to call it blob. py it's I don't know we'll call it blob. I guess let's do blob. so with blob. pi um what we're going to say is we're going to take blob. Pi we'll open that and blob. piy will literally only consist of class blob and I'm contemplating at least giving this random let's go ahead and give it random that seems fair enough so we'll give it random so we'll save that close that and I'm just going to move this aside now we don't need that still and what I'm going to change or give over here is we're going to get rid of The Blob class and instead let's just import it so from um blob we'll import lob and you should already guessed this isn't going to work but let's go ahead and kind of work through um why it's not going to work and in fact I'm already like like wondering am I did I delete more than blob no oh okay there it is wow I'm blind I was wondering where width and height were and they're right there okay fantastic let's run it see what happens okay the first thing we get is no module um uh no module name blob oh I see what we've done so this was already a pi file uh so so blob it needs to be blob. Pi but it is but what I did was blob. p.p anyway continuing along okay so the first thing we get is the the error that I already kind of alluded to and that's that width is not defined well of course right so so if someone else was trying to use this and in our our blob class we have um def we've basically said hey we need to use this uh this width and height well that's not accessible to anyone else right so probably what You' want to do is have some sort of boundary right up here um so what we could do is maybe when we initialize this blob we give it some sort of range or at least require some sort of range so we might say um I'm trying to think if I want to use something other than width and height but I cuz like width and height might seem like class blob so we could give it like um we'll say x boundary and then y boundary boundary okay so now what we'll do is uh self. X Bounder boundary equals x boundary and then self.y boundary equals y boundary okay so we'll take that and basically replace width with X boundary and height with Y boundary fabulous now we obviously need to come down to where we actually call the blobs so now you've got blob blue and we need to uh also add width and height here so blue width height and red width height okay let's run that are we still oh that's where else did we screw that up was that oh we just set it on the on the uh the maximums uh so we still need to cover obviously width and uh height here so let's actually cut that paste and let's do this and this okay looking back through here are there any no so we'll save that and let's come back over to modularity rerun it okay and things actually work in this case great now uh closing this out this so there's a couple of things like for example with this blob class you know maybe X boundary and Y boundary you might not even really want that you might really think more of like like you might not actually want the block like the blobs ma minim Max to actually be here you might want to say like okay the range is for for the starting point or maybe let the user say where does this blob actually start so let the user set an initial X and Y value right so you could do that you can also let the user decide if they want to have this kind of arbitrary boundary rather than you the programmer saying yes we want to have that now I think in the case of The Blob and in the with the understanding of python and py game like you can make the decision that you want that to be the case like you just you just know that because of the way P game works there's no like really zoom in and out out or anything like that so we do want the blobs to remain on it within these bounds also you might think about you could like set really large default boundaries or something so it probably wouldn't be a problem for people but anyway that's beside the point um generally you'll want to keep any kind of hardcoded values out as well so like with self. size you might want to have like size range as a parameter so you might want to throw in size range size range and then you could set a default if you wanted so we could say uh somewhere between 4 and8 and then uh in this case you wouldn't need to say set a self. side size range you could just replace this and this uh these zeros are acceptable that's not really a problem and then the movement there's a couple of things you could do here you could set some sort of Max movement speed uh or maybe you could take it upon yourself as the creator of The Blob class that um nope this is is as good as it's going to get uh we don't want uh people to be able to move the blobs faster or something like that so in the case of a blob class obviously you probably want to give them some Freedom like people using it but there's a lot of other scenarios where you actually wouldn't want to give people freedom but at least in this case I think that you would probably want to give people the ability to change that so maybe give um let's say movement range and then this one again uh -1 to two and then we'll take movement range and actually that needs to be equals movement range equals 1 to2 and then take this and that should be a zero zero one and one save that and then now someone can just call blob and make the decision okay do you want to pass those other uh parameters or do you want to just use the defaults and in many cases they'll just want to let's see movement range oh I see what we've done let's see at least I think I have yeah so to access and move here we obviously we need to set a self. movement range self. movement range equals movement range and we'll take self dot pass that in there save I don't want to close that yet and let's run that great so now um anytime you know when when someone wants to use the blob class it can be either as simple or as really as complex as one can make a blob class anyways because it is such a simple thing um but now we don't really have any hardcoded things besides this zero which maybe you you might change maybe you're going to say boundary range like boundary X range boundary y range or something like that and then you're getting rid of these zeros and then if you were going to do that you would for sure get rid of these lines um I'm still on the fence as far as whether i' want to leave that in the class or not I think it's a pretty valid thing to leave in there but probably it would be better to let the user do that so um so for example you might say like that and then coming over here when they run a blob. move you you might do something like this so rather then you saying okay this blob cannot ex cannot exceed the boundaries the user can say no this blob cannot exceed the boundaries so maybe like this what is this that's gone now oh I see okay so uh pl. XY there okay so we replace all of those and I can't remember if we saved this one or not yes so we'll run that and hopefully this guy's going to yeah so he seems to be staying out so we're controlling that now as as the user of The Blob class rather than the blob class actually enforcing this upon us and again there's going to be times where as the the class Creator there are going to be some things that you're going to want to enforce upon your user and interesting that was still there let's save that one more time we were like double doing that still appears to be working but anyway um there will be times when when you create something and you do want to force something on a user because maybe maybe you're trying to protect a user from be uh violating a best practice or something like that sometimes you want to do it also like if you're some sort of data analysis Library uh and you're like a c optimized data analysis Library like numpy or maybe you're pandas and you're using numpy uh and you know you know the best way to do something then maybe maybe you even rather than move you might add a method to The Blob class that checks for a boundary um for exceeding a boundary so maybe you know the best thing is to just do this code and so maybe you leave that here but maybe the method is uh Define check bounds self and then you might save that come back over here and then instead the person if they want to use your um your method they might say okay uh let's go ahead and blob. check bounds save make sure that one's saved should be good uh to find check bound self did we forget it yeah for a cool one run it again great I'm just waiting to make sure nobody exceeds but anyways um those are just some some initial things to think about we actually we have a long way to go when it comes to uh things you should be thinking about when you're creating classes and eventually um I'm going to do my best to convince you that you probably need to be doing something completely different before you even write your class entirely but we'll get there at some point uh but at least for your simple classes probably the big takeaway here is that you'll want to think about you don't necessarily have to I'm not a huge proponent of taking your your your your programs or whatever and just turning them into these huge libraries but I am a big fan of making sure your code can be modular uh if and when possible and it's too easy when you write it all in line with your main whatever your main execution is it can be too easy uh to do it all together and wind up using things like your constants and stuff like that and not realizing that that's not going to be acceptable for either you to use the code later on or for anyone else to use your code and then also when you do stuff like this uh just try not to try to impose as little as possible on to whoever your user might actually be of the program let them make up their own minds but you can always be you can always do something like this where it kind of like helps them but you probably wouldn't want to like force them to stay in bounds in the move uh the move method but give them the option to maybe we check boundaries or something like that and then you could even it might even make more sense that whe rather than having X boundary up here you might want to have the boundaries down here cuz otherwise these are not necessary I don't think let's see XY Rand range well I guess in the in the creation of where you want the X and Y to be yeah so I guess you would still have to have it up there but anyways lots of things to think about uh lots of choices we could have made on the code that's the thing with programming I mean there probably isn't arguably best way to do it but probably not I don't know who knows uh but there's a million ways to do something but there are certain things that you definitely want to be thinking about as you go so anyways um that's it for this one one if you have questions comments you got better Wy that you might think that we should uh bring this blob class into being uh let us know below otherwise I'll see yall in the next tutorial

Original Description

Welcome to part 16 of the intermediate Python programming tutorial series. In this part of the series, we're going to discuss the modularity of the class we wrote. 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

This video teaches the importance of object modularity in intermediate Python programming, covering topics such as class design, initialization, and modularity, with tools like PGame and Python. It provides hands-on experience with coding and designing modular classes.

Key Takeaways
  1. Copy and paste the blob class into a new file called blob.py
  2. Import the blob class from blob.py
  3. Replace width and height with x boundary and y boundary
  4. Initialize the blob class with x boundary and y boundary
  5. Call the blob class with x boundary and y boundary
  6. Cut and paste code to modify the blob class
  7. Run the modified code to test the changes
  8. Set default values for parameters
  9. Control movement range by the user
💡 Object modularity is crucial for code reusability and maintainability, and using constants and variables in classes can make the code more modular and flexible.

Related AI Lessons

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