Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:48Length:7.6 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

5.1 Modules

As you begin to see more Python code in the wild, you will see more structured code. So, let's look at the most basic way to break Python code down into functional units—modules—and how to reuse that code in other parts of your application.

1.Introduction
2 lessons, 11:32

1.1
Introduction
02:03

1.2
Prerequisites
09:29

2.Python Building Blocks
6 lessons, 1:08:07

2.1
Introduction to the Interpreter
09:51

2.2
Numbers
10:53

2.3
Strings
14:36

2.4
Lists
11:33

2.5
Standard Input and Formatting
12:00

2.6
Building a Tip Calculator
09:14

3.Controlling the Flow
7 lessons, 1:20:10

3.1
Conditional Statements
12:47

3.2
Looping With For
09:36

3.3
The Range Function
10:41

3.4
Looping With While
13:06

3.5
Creating Functions: Part 1
11:55

3.6
Creating Functions: Part 2
08:49

3.7
Building an Average Calculator
13:16

4.Common Data Structures
4 lessons, 46:49

4.1
Lists, Stacks, and Queues, Oh My!
11:45

4.2
Dictionaries
10:10

4.3
Iterating Data Structures
09:41

4.4
Building a Sentence Analyzer
15:13

5.Application Structure
7 lessons, 1:15:12

5.1
Modules
09:08

5.2
Packages
11:24

5.3
Classes
09:53

5.4
Attributes
09:34

5.5
Methods
12:01

5.6
A Special Calculator: Part 1
13:36

5.7
A Special Calculator: Part 2
09:36

6.Collections
7 lessons, 46:55

6.1
What Are Comprehensions?
06:32

6.2
List Comprehensions
06:08

6.3
Dictionary Comprehensions
06:38

6.4
Map
05:45

6.5
Filter
06:31

6.6
Lambdas
05:21

6.7
Generators
10:00

7.File I/O
6 lessons, 48:51

7.1
File Basics
06:50

7.2
Reading Entire Files
07:49

7.3
Navigating a File
08:32

7.4
Writing to Files
07:22

7.5
Reading and Writing to Files
09:15

7.6
Reading and Writing Complex Objects
09:03

8.Networking
5 lessons, 43:48

8.1
Introducing the Socket
04:39

8.2
Getting a Remote IP Address
06:42

8.3
Handling Socket Errors
07:58

8.4
Create a Socket Server
16:04

8.5
Create a Socket Client
08:25

9.Connecting to Network Services
3 lessons, 34:27

9.1
Getting the Current Time With NTP
10:38

9.2
Getting Websites With HTTP
12:57

9.3
Downloading Files With FTP
10:52

10.Conclusion
1 lesson, 02:08

10.1
Goodbye
02:08


5.1 Modules

In the last section of this course we're gonna talk about how we will be able to logically structure our applications and our code that we have within Python in a way that's gonna make sense for not only us, ultimately other users. So the first thing we wanna do is talk about structure, so how do we structure things within Python? Now Python is a little bit different in terms of how the structuring typically takes place. A little bit, so the way that your typically going to kind of break up functionality and put different pieces of functionality in different places is basically file based, so your gonna put some sort of functionality. Whether it be just lines of code or lines of executable code or defining functions or ultimately classes, you're gonna put those in specific files, maybe named in a certain way that makes sense ultimately to you within your code. And hopefully those names make sense to others along the way as well. But how do you ultimately use that functionality from different applications or from different pieces of code? And so in order to understand that we're gonna start talking about these logical structures within Python known as modules. And really a module is just a file, like I talked about, that contains these statements or functions or what have you in Python. And it really is just a file that has some sort of name, and then, it has a suffix, PY, appended to the end of it. And that's really it. So, we really created modules. We've been creating modules. This entire course, when we create these little applications and create these little sample bits of code, those are all modules. But let's go ahead, and start down another example, so you can really start to pay attention to the different pieces and how this works. So let's say we had a bit of code here, and we're just going to define two functions. We'll say, say hello, which is really not going to do much else except for print the string hello. Like that, and then we'll do one more that's going to be, say goodbye. And once again that's not going to do a whole heck of a lot other than print goodbye. Really, right now all we've done is we've created a very simple module that contains two functions in it, say hello and say goodbye, and that's about it. In order to actually make this a module, we need to save it and give it a name, so let's go ahead and save it now. And this we'll just call conversation. Something along those lines but the important part here is that we're going to add the .py suffix to the end. Now we have actually created this concept of a module. Now, as before, when we were wanting to execute this. We would have to actually write some code in here that would say execute. Call the function say goodbye or call the function say hello, so that when we actually came in here and executed this and said conversation, we would actually get it to say something out here. But what if the ultimate purpose of this module is not for it to actually do any sort of execution, but to just contain an implementation that is going to be used by some part of our application, and hopefully be reused by parts of someone else's application. Ultimately how do we use this, now a little bit of a hint is we started to talk about this in a previous lesson a little bit when we imported a module from somewhere else, and if you remember how that worked it was actually quite simple, we just used an import statement, or a variation actually, of an import statement. So let's go ahead and check out how something like this would work. We're going to go into our interpreter just cause it's quicker and easier. Ultimately what I would like to do is, let's say I'm running some other application and I'm where I'm writing some other application, and I wanna use this functionality here that's found within my conversation module. The more you start using this terminology, the more you're going to better be able to have conversations with other developers that write using Python. So I'm working with the conversation module. And what I wanna do is I wanna share that functionality with someone else. And they want to import that functionality Into their application somehow or into their code. The way that we would do that is very simply using our import statements. We use the import statement. And we're going to import conversation, and the reason we give that name is because we want to import a specific module. We're going to import the conversation module. And when you do an import, you import the full name of the module minus the extension here. We're gonna say, I want to import conversation. As you can see, that seems to have worked. So if you were to guess at how this was going to work now you might think, well I just wanna use the say_hello functions why I want to do say hello, and let's go ahead and run that. But the interesting thing here is that we're originally not, [LAUGH] that's not going to work. Because the way that we imported this module, by using just the import statement and importing this module, we did not enter the names of the functions or anything else that's going on with this module into what's known as the symbol table. And I'm not going to go into great detail as to what that is. We'll save that for another course, but really, that is just something that keeps record of all the things that are accessible from where we are at currently within our application or ultimately within the interpreter at this point. So, right now just because I imported conversation, I have imported everything that we have found. But it's all kind of hidden In this concept of conversation. We've actually only entered the conversation module into that symbol table. If we wanna get to any of this functionality within that module, we first have to start with the name of the module, but now I have access to that at least. So, I could say conversation.say_hello, and now we're gonna get hello. And I could do the same thing with goodbye just like that and we would get goodbye. So that is one way that we are able to create some functionality within our application, save it into a module, into a Python file. And then we could use it in other areas of our application, whether we are running it from the interpreter here like this, or if we were just putting input statements at the top of our file, I could import other modules at this point from some other module somewhere within our application. Or something that someone else has given us, we can import that functionality, and then we can go ahead and use it wherever we need it. We started to talk a little bit about this concept of symbol tables. Now one thing that is nice that Python also allows, and this is kind of a variation on the import statement that we kind of touched on a little bit in the previous lesson, or in one of the previous lessons, was we did a variation of the import statement. I'm gonna go ahead and restart my interpreter here just so I can start with a little bit of a clean slate. Now, if you remember from that previous lesson I did an import statement, but I specified a from beforehand. And what the from allowed me to do was to specify that I wanted to say from a particular module, and I'm gonna give the name, I'm gonna say conversation. From the conversation module, I can import just bits and pieces of what I need. So I can say from conversation, let's say I only wanted to import say hello. I could say from conversation, import say_hello, so now I can import just that one specific piece of functionality. And an interesting thing about this is now that I've done that import statement, I can actually do say_hello, and I'm actually going to get that functionality directly in my symbol table, because now instead of actually importing this entire module into my symbol table, I'm only importing exactly what I'm asking for. I can say from a particular module, in this case conversation, I only want to import say hello. So now if I were to try to do, say goodbye, as you see this is not going to work because I didn't actually import it. And if I wanted that to work, I would actually have to go back and do another import. I want to import say goodbye, and now if I were to execute that it would work. So as you can see, this is a nice way to start to introduce the concept of kind of modularizing your application into different pieces of functionality and saving that the related pieces of information into these modules. That I can kind of import them as I need them, because you will need all the functionality all the time. And if you kept everything in a single file It would start to get very long, and very large, and very difficult to maintain, and difficult to understand. So this is a nice little way that you can start to group like functionality into these files, into these modules. And then just import them as you need, whether you need to do all of them with a straight import statement or using the from import statement where then you can only bring in certain pieces of functionality from a specific module that you're going to need right now. So that's the first step in understanding kind of how you can break things apart within Python. And now in the next lesson, we're gonna start talking about another similar concept, or a related concept, called packages.

Back to the top