Effective 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 is a little bit of an advanced feature
0:10 and it's not entirely a perfect match for what we're doing here. So we just have a bunch of files hanging around in this wizard directory
0:20 and it's just because of the working directory, they all find each other. If we had created actual package,
0:27 we would have done things a little bit differently. Now, if you haven't created a package, what makes a thing a package is really super simple
0:35 and Python uses this to behave differently, so if we had a file that was __init__.py and it was empty,
0:43 I am going to say no for now, I don't really want to do it, this is sort of the main entry point to the package
0:51 and it can be empty, it doesn't have to do anything, but the existence of this file in a directory is what defines a Python package
0:59 and then you can put implementation details here. That's different than say a module and you get many different subfiles and whatnot.
1:06 It is different than a module I say actors which contains creatures but it's just a single file, not a group. So, what we can do is we can say
1:16 if we like I can convert actors into a package, So instead of being a single Python file, it's a directory with __init__
1:25 and potentially many other files, all within that directory. So, watch this, so if we operate on a particular file
1:37 rather than on parts of code or whatever, we could say convert to Python package because it's a module,
1:45 and what it does is now it creates a folder called actors, the folder is the name of the module,
1:50 and the __init__ can, it doesn't have to, but can contain the implementation. So you can see the actors.py is gone, the implementation is all put here
2:01 but we could create a bunch of subfiles, import them here, break it apart and we probably should create a creature class, a wizard class and so on.
2:09 So that's pretty awesome, if we decide we have this situation, we'd really just like a file, we could do it in reverse;
2:17 go back from the directory with the __init__ and the content in there to just a simple single file actors.py, the editor on the right didn't change,
2:28 but the name of the file and structure and what it means to Python did.


Talk Python's Mastodon Michael Kennedy's Mastodon