Mastering PyCharm Transcripts
Chapter: Refactoring
Lecture: To and from packages

Login or purchase this course to watch this video and the rest of the course contents.
0:02 Now this final refactoring technique I want to talk about
0:05 is a little bit of an advanced feature
0:09 and it's not entirely a perfect match for what we're doing here.
0:12 So we just have a bunch of files hanging around in this wizard directory
0:19 and it's just because of the working directory, they all find each other.
0:23 If we had created actual package,
0:26 we would have done things a little bit differently.
0:29 Now, if you haven't created a package,
0:31 what makes a thing a package is really super simple
0:34 and Python uses this to behave differently,
0:37 so if we had a file that was and it was empty,
0:42 I am going to say no for now, I don't really want to do it,
0:47 this is sort of the main entry point to the package
0:50 and it can be empty, it doesn't have to do anything,
0:52 but the existence of this file in a directory
0:56 is what defines a Python package
0:58 and then you can put implementation details here.
1:00 That's different than say a module
1:03 and you get many different subfiles and whatnot.
1:05 It is different than a module I say actors which contains creatures
1:08 but it's just a single file, not a group.
1:11 So, what we can do is we can say
1:15 if we like I can convert actors into a package,
1:19 So instead of being a single Python file,
1:22 it's a directory with __init__
1:24 and potentially many other files, all within that directory.
1:29 So, watch this,
1:32 so if we operate on a particular file
1:36 rather than on parts of code or whatever,
1:40 we could say convert to Python package because it's a module,
1:44 and what it does is now it creates a folder called actors,
1:47 the folder is the name of the module,
1:49 and the __init__ can, it doesn't have to, but can contain the implementation.
1:54 So you can see the is gone, the implementation is all put here
2:00 but we could create a bunch of subfiles, import them here,
2:03 break it apart and we probably
2:05 should create a creature class, a wizard class and so on.
2:08 So that's pretty awesome, if we decide we have this situation,
2:13 we'd really just like a file, we could do it in reverse;
2:16 go back from the directory with the __init__ and the content in there
2:22 to just a simple single file,
2:25 the editor on the right didn't change,
2:27 but the name of the file and structure and what it means to Python did.