Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Modules and Packages
Lecture: Pythonic import statements

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Let's talk about import statements. Over here we have a variety of things being used
0:06 that should come out of modules that are mostly in the standard library but some of them we build ourselves.
0:12 So you can see we're using the system module, we say "sys.versioninfo.major" so this should print out "3" the way we have our system setup right now,
0:20 of course you can see there is an error here and we should come over here and say "import sys". So this is one way we can do this,
0:28 and this is certainly considered Pythonic, it's using namespaces, right, so "sys.", we know where this version info is coming from,
0:37 and remember, namespaces, those are one honking great idea, let's do more of those. OK, cool, so we are doing more of those here, now sometimes,
0:47 you don't want to have to say the namespace or the module name here, and especially this really long like "SQLAlchemy.orm.ext.declaration.",
0:57 this is maybe a little bit long, but sys, sys is great. However, if you would like to use a type over here we can say "import os"
1:07 and we could come over here and we could say "os.path", but we'd rather just say path, so instead we'll say "from os import path".
1:15 We can do it like this and this is also considered very Pythonic, there is a shortcut way, we could get hold of, here we are using the stats module,
1:25 statistics technically, we'll rename it, so we could get hold of median, the mode, the mean and all those things,
1:31 if we said "from statistics import", rather than naming them, the correct way would be to say median mean and so on,
1:41 name them, but we could say, let’s leave this here and I'll comment it out, we could just say you know, I'll just use everything, that's cool thanks.
1:49 Well, you can see all these 3 lit up this is considered to be not Pythonic at all, let me show you why.
1:56 So we have this, suppose we want to use median and mean from the statistics module, and let me go ahead and import this as well,
2:02 so we'll say "import statistics as stats", so here if we want and if we had something really long,
2:09 like I said "SQLAlchemy.orm.declarative. da da da", we could rename it, so here we could say "import statistics as stats",
2:16 and then just say "stats.", so kind of taking over the namespace, to greatly simplify it. Suppose mode here is not the statistics mode,
2:24 but we want to set the mode of operation of our program, so we have kind of a crazily named file here, beware_the_trade_imbalance support file
2:35 and in there, there is a mode function, you can see "def mode", it prints, that really didn't do anything,
2:41 but under one condition it prints "Mode set to DEFAULT" or "Mode set to ADVANCED".
2:45 So when we say "mode" down here, this is actually the mode we want to use, so I can come over here and say OK, cool,
2:50 so I'll say "from..." we want to import mode. Now, PyCharm just because of this weird naming convention I had here, thinks this is a private thing,
3:03 so let me just tell it: "Don't make this look like an error for us." Because normally that would maybe mean "protected" but it doesn't in this case.
3:12 So, now all of our code should run, we print out the major version, the current path,
3:18 the median of those numbers, again, that should be the same median, then the mean, so the median, median, mean, and just like we would expect
3:27 we have our mode set to advanced. So our mode is set to advanced. The None is coming from, let me actually, just this doesn't return the value
3:35 in that case we wrote. OK, so that's the way it worked, but suppose instead of doing this Pythonic style of importing without the namespace,
3:42 I said you know, I just don't want to worry about it, let me, I want to have a bunch, let's just get them all, so we can go like this.
3:49 However, even though I was not using the mode from statistics, this imports mode from statistics, flat out, and all the other ones,
4:00 I am not sure how many are in there but many of them. Now notice, PyCharm knows something is amiss here.
4:06 This has gone gray and says, "you are not really using this mode", that should be a warning that something is wrong,
4:12 so if I remember this said "mode set to ADVANCED", now If I run it, mode is seven, what happened? We imported mode and then we reimported mode,
4:22 which replaced its definition in this module. So mode is gone. All right, we never intended to use mode from statistics,
4:29 we just wanted to be lazy and not import those two separately, this is bad, don't do this. Notice when I comment this out, this gets enabled again
4:39 and let's do one more thing, notice how this got some errors and these are undefined, and I did type it up here but let me remove it for a minute
4:47 and just show you the PyCharm can fix this, so if I go over here I can hit Alt+Enter and it will say,
4:51 now because I have the stats up here already it knows, it's trying to help too much, so if I come over here and I hit Alt+Enter,
4:57 it'll say "import statistics.median" and I hit this, great, and again Alt+Enter, you can see,
5:04 it's doing the Pythonic way of doing the import here for us, perfect. And of course, let's put our stats back because we were confusing PyCharm there.
5:12 Let's see this in a graphic. So namespaces are great, "import sys", that was cool, importing bare items by name, that's fine, "from os import path",
5:24 we could rename or shorten our namespaces, "import statistics as stats", maybe it doesn't make sense in this case,
5:31 but I will give you some examples where it does, and we also saw that we have to be very careful
5:36 with wild card imports, "import *", because the two lines above, we are trying to get mode from our custom package and mean and median from stats
5:45 and if we wanted to get lazy and say "forget mean, median, and we should write *", we would blast away our custom packages mode definition.

Talk Python's Mastodon Michael Kennedy's Mastodon