Your Opinion(s): One file, a few, or many?

How many .py files would you use for development of a large game?

  • One file.
  • A few files based on logical grouping of content (classes and functions for movement…).
  • One file per class.

0 voters

Pretty much all examples given on this board, as well as the samples, provide single .py file solutions. I understand this is done for simplicity and I’ve assumed, like with other languages, multiple files make more sense for large development. However, taking the sample “Roaming Ralph” and breaking it down into separate files has lead me all over the internet for python class tutorals. What I’ve found is that there are a number of posts suggesting that python code is simplistic enough that it tends to lend well to single file projects. I’ve also noticed that some people break their code into files based on classes, while others are placing sets of functions, not necessarily with a class, in separate files.

I understand this is mostly a developer preference, but is there a panda3d reason to choose certain path and what about this function vs. classes in separate files, any performance benefit? Why classes, when functions seem equally powerful in python?

I tried to treat python modules (single file) as packedges in languages like java. I think module should contain couple of classes unless the class is very big then just one is enough.

So your preference is to only place a full class or classes in the extra files? The first example I ran across when looking for a tutorial on how to place classes in separate files actually showed just functions being separated:


from import somedef
print somedef()

def somedef():
   return a

See, no class or constructor “def init(self)” in the file. Doesn’t that make it just a function (what’s the point!)? I don’t see a good reason to avoid class usage but I thought maybe I was missing something about OOO in regards to python. I noticed you referred to the separate files as “modules”, a word another well known poster used when referring to separate panda3d/python files. I will do some searching in that direction.

I know that classes in separate files are a python/programming concept (meaning not panda3d specific), but it would be nice to see some of the samples like “Roaming Ralph” presented in both single file and separate class files. Particularly useful would be the passing of “self” references from class to class and the proper calling of class files from the file. When I was reading through the wiki, I thought I saw mention of how to use separate files but I haven’t seen it since that thought. Time to re-search that again… =)

Thanks treeform!

This is how I have done it in the past i dont think any one can give you any thing better then their preference:

Hi, after reading this I got all happy lol. Maybe I am miss reading it or understanding it, but you can inport other files to work with the main? Like lets say I have a game with many levels and eatch of the level have its base code into its own file so when lets say:


"" -Main menu<------------------------------------
         |-load game states and files                   |
         V                                              |
file ask for file ""                            |
        |              |-->player ask to see menu again--
        V              |-->load
in zone to ""


Can it do this?

This might work on small scale games. My own game is huge – it has over 25 long classes I think, when I group them together it becomes one large mess.
What I do however, I keep one file per class, or maybe two small strongly-related classes together, and put them in subdirectories (like gui/ for all GUI classes, net/ for networking classes, etc.)
Then I create files and simply do (from gui import Menu).

could you elaborate on this plz?

I have a lot of GUI code I want to put in a separate file & import into my main .py

what would the import call be?

from blablabla import ???

It’s a bit more simple :slight_smile: and powerfull.

Basically , in python you can map modules to file folder very fast.

If you have a folder that includes files and files that include classes,
then it is sufficient to get create an empty file into this folder to transform the folder in a module.

Folder mymodule

so i you want to use class1 in your code, then you can simply do
from mymodule import class1

or import mymodule

You can later customize your init file to include some help , some class access restriction and so on.

I would advice to read “Dive In Python” about module and classes. There is some neat things you can do there. Keep in mind that in python, module and class name and file name are case dependent…

thanks a lot