Or, Is it really important? Or, How it will affect our programming approach? Answers to all these questions are within this article. First, understand why is it needed? In programming, we are often in situations in which we have to repeat certain operations. Writing code for them each and every time is quite a tedious task. OOPs is a programming concept in which we create objects based on some defined classes having some properties called attributes and functions called methods.
The advantage of doing that is we can reuse it a number of times we want. We can also implement other real-time entities like inheritance i. It is an independent concept in itself, so we will talk about it somewhat later.
For now, move ahead and talk about modular programming. Besides the core concept of OOPs, there are some other problems we face while programming. Working on real scenarios is very different from the one that we learn. In reality, solving any problem may require thousands of lines of codes which may have hundreds of functions and tens of classes.
Maintaining all those functions and classes in one program is a difficult task. So we use the modular programming approach to cope with such situations. Modular Programming is a programming approach in which rather than writing code for one big problem, we split it into several small independent problems. Once we solve each of them, we unite them to achieve our primary goal. The advantage of following the approach is that we can increase the readability of codes.
You can import module1. Just using the following command. There is no limit to the number of classes you can add, but for the sake of this article, well stick to one. In order to call a function or class belonging to module1, that function or class must be preceded by the word module1.
This helps any name collisions that may occur if there is a pre-existing function or class with the same name. Importing the module as we mentioned earlier will automatically bring over every single class and function in the module into the namespace.
You wonder, why bother creating our own modules when their are already so many available? The answer is custom libraries. To create a GUI, you need to write dozens of lines of code even for the simplest interface. At some point you realize that it would a better idea to create pack this code into a function and save it in a python file.
Later when you want to create a GUI, you can simply call that function with a single call. This approach is usually meant to be a personal one. Are classes in Python in different files? Ask Question. Asked 11 years, 8 months ago. Active 11 years, 6 months ago. Viewed 86k times. Improve this question. Asaf Asaf 7, 17 17 gold badges 62 62 silver badges bronze badges.
Duplicate: stackoverflow. Add a comment. Active Oldest Votes. To instantiate an object, you have to import the class in your file. Improve this answer. Ned Batchelder k 69 69 gold badges silver badges bronze badges. Felix Kling Felix Kling k gold badges silver badges bronze badges. Ok, since it's called a module, do I need to import it?
Asaf: See my updated answer. How you have to import depends on different things. If the modules are in the same package you can just import the module. If not, you have to import the package too. Never include the file suffix i. Read the tutorial and search here on SO about how to import correctly.
This question was already asked.
0コメント