0

I am wondering if it would be a good idea to use different .py scripts for different parts of a Python program. Like one .py file for a calculator and another for class files and etc. If it is a good idea, is it possible?

If possible, where can I find how to do so?

I am asking this because I find it confusing to have so much code in a single file, and have to find it anytime fixing is needed.

nbro
  • 15,395
  • 32
  • 113
  • 196
  • Depends on the size and scope of your program. In general if something can be re-usable in other contexts it belongs in its own module: https://docs.python.org/2/tutorial/modules.html – Matt Coubrough Dec 18 '14 at 20:57
  • 1
    This is very good idea. It will keep you brains from exploding, when you open one big .py file (module) with all components of big system. Separate modules for specific functionalities - is a pro choice. – Gill Bates Dec 18 '14 at 20:58
  • Probable candidate for programmers.stackexchange.com, seeks an opinion (although it's a very common question for beginners_ – kdopen Dec 18 '14 at 21:11
  • This question appears to be off-topic because it is about having not read the most basic documentation. – Mad Physicist Dec 18 '14 at 22:05

2 Answers2

1

If your program starts assuming big dimensions, yes, you could split your classes or simply your functions according to what they do. Usually functions that do similar tasks or that work on the same data are put together.

To import a file containing a set of functions that you defined, if the file is in the same folder where your main script is, you can simply use this statement, if, for example, the name of the script containing your function that you want to imported is called functions.py, you can simply do import functions or from functions import *, or, better, from functions import function_1.

Let's talk about the 3 ways of importing files that I have just mentioned:

import functions

Here, in order to use one of your functions, for example foo, you have to put the name of the module in front of the name of the function followed by a .:

functions.foo('this is a string')

from functions import *

In this case, you can directly call foo just typing foo('this is a new method of importing files'). * means that you have just imported everything from the module functions.

from functions import function_1

In this last case, you have imported a specific function function_1 from the module functions, and you can use just the function_1 from the same module:

function_1('I cannot use the function "foo" here, because I did not imported it')

nbro
  • 15,395
  • 32
  • 113
  • 196
  • So if I had a menu in another .py file, how would I call it and use it in the main program? I know I would do import menu, but what would I do to use the code from that? –  Dec 18 '14 at 21:38
  • @AustinHargis When you import your module called `menu.py` with this syntax `import menu`, you are actually importing all the code from that same module, so you can just call the functions or variables from the `menu.py` just as I explained in my answer. – nbro Dec 18 '14 at 21:41
  • Oh, alright, I see. I was slightly confused by your answer originally but I see now. Thanks :D But when I go to convert my program to .exe (I use cx_freeze) how would I do that since my program would now use multiple .py files? –  Dec 18 '14 at 21:51
  • @AustinHargis There's an option, in the `setup` file that allows you also to say to `cx_Freeze` to check for the other modules. Check out this post for more information: http://stackoverflow.com/questions/2553886/how-can-i-bundle-other-files-when-using-cx-freeze – nbro Dec 18 '14 at 21:52
  • Alright thank you. I am having some issues though. I don't know how to pull information from classes with this method. I have one class with name and highscore. I am trying to access that information from a different script with: game_state['players'][0].highscore & game_state['players'][0].name. How can I pull this info from a class? Sorry I don't understand much. –  Dec 19 '14 at 00:05
  • @AustinHargis I recommend you to watch some tutorials on OOP paradigm ;) – nbro Dec 19 '14 at 00:18
0

It certainly is possible, and is frequently a good idea. This is for exactly the reason you cite: if you have all your code in one big file, it gets annoying to edit that one file.

So, for example, let's say you have a calculator function and some class. You can put them in two separate files -- the first in calc.py

# calc.py
def calculator(a,b):
    return a.do_something_clever(b)

and the second in some other file

# cleverness.py
class Cleverness(object):
    def do_something_clever(self, c):
        return 3.14159+c

To use them from some other script, you just need to import them:

# cool_script.py
from calc import calculator
from cleverness import Cleverness

c = Cleverness()
print(calculator(c, 2.717))

Instead of from x import y, you can also use import x and then x.y. For example, the following is equivalent:

# cool_script2.py
import calc
import cleverness

c = cleverness.Cleverness()
print(calc.calculator(c, 2.717))

There's always a balancing act between making too many separate files and making too few. Basically, it's up to you. The main factor that drives my decision is usually how closely related the different things are, and how reusable the different components would be if they were separated. For example, if my calculator function did something complicated, but didn't depend at all on the details of Cleverness, I would make sure to put it in a separate file. On the other hand, if I always import Cleverness whenever I import calculator, and the codes for both were simple, I'd probably just stick them into the same file. And if I'm never going to use the code in them again, I'd probably just stick them in the main script's file itself.

Mike
  • 19,114
  • 12
  • 59
  • 91