1

I am an advanced programmer who recently started using and learning python. I recently ran into this question. The answers in that question suggests that instead of using a static method inside a class, it is considered better design to just use a method and not use a class at all.

Code design 1:

gameResolution.py

width = 300
height = 300
# other variables, and functions following...

screenResolution.py

width = 1920
height = 1080
# other variables and functions following...

Code design 2:

resolution.py

class GameResolution:
    width = 300
    height = 300
    # static functions following

class ScreenResolution:
    width = 1920
    height = 1080
    # static functions following

As you can probably see, Code design 2 makes a lot more sense (in my opinion) in this situation. I could pack anything related to that entity (GameResolution or ScreenResolution for example) inside a class and all those classes inside a resolution.py file.

If I followed the Code design 1 I would end up with many tiny .py files, that would each represent a very small class. Correct ?

So, is it actually true that static methods are a bad approach for python ? In this situation, what would you do ?

Community
  • 1
  • 1
dimitris93
  • 4,155
  • 11
  • 50
  • 86
  • It would be even better to make `width` and `height` the object attributes, and the methods non-static. Then you can create multiple `GameResolution`s etc. – bereal Jul 04 '15 at 14:15
  • @bereal There can be more variables as I imply with `# other variables, and functions following...` that do no match, so making a `Resolution` class is not really possible. However since they do share a couple of common features it makes sense to keep them inside a common file `resolution.py` in my opinion, instead of making a douzen small `.py` files – dimitris93 Jul 04 '15 at 14:17
  • 1
    I don't see how having other variables and functions can not match, if you design the whole thing in OO way. – bereal Jul 04 '15 at 14:19
  • If your class is truly one-off and you can't figure out how to generalize the class to be useable more then once then it's not pythonic to use a class. You can of course do whatever you please, I'm not sure that it's going to take any sort of performance hit or that you'll run into any particular problems, but other python programmers will particularly like the code. – CrazyCasta Jul 04 '15 at 14:22

1 Answers1

2

The best solution depends on your style a bit. That being said, I don't think making classes that as a single use static are the way to go. I would do one of the following:

  1. Make a class that handles all the resolution stuff (GameResolution/ScreenResolution/etc) and make separate instances of it for each.
  2. Change the names of width and height to game_width, game_height, screen_width and screen_height.

As for when I use static methods, about the only time is for creating objects in a way different then the initializer for some reason. The need to do such is fairly rare though. It definitely sounds like your use case is better suited to either one class to represent everything, or if they're different enough then globals and functions instead of classes and static methods.

CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
  • This is helpful, thanks a lot. The 2 "resolution" classes are different enough, so I can't just make a common `Resolution` class. So my example with the resolutions was a pretty bad example. However as you say `globals and functions instead of classes and static methods` is what everyone seems to be using and you also say it is the way to go, so it has to be – dimitris93 Jul 04 '15 at 14:29