0

In writing modules for python code to improve readability and portability, is it better practice to write methods or variables for simple return values? Why? Do python methods utilize more space in memory or do variables?

In practice it doesn't change much... module.item() vs module.item Note: Consider readability of code that would be written using the module. Not the readability in the module itself.

For example:

#colors.py

def red():
    return [255,0,0]

def green():
    return [0,255,0]

def blue():
    return [0,0,255]

VS

#colors.py

red   = [255,0,0]
green = [0,255,0]
blue  = [0,0,255]
  • 1
    Why on earth would you write those functions? I would be inclined to use tuples rather than lists, partly for mutability reasons (or `namedtuple`s so you could access the values by e.g. `colors.red.blue` if needed), but otherwise the second version makes far more sense. – jonrsharpe Mar 26 '17 at 14:36
  • No clue if it would be worth writing functions like that. Hence the question. Just for the sake of knowledge what others would say/do. –  Mar 26 '17 at 14:38
  • 2
    Try to debug using memory_profiler https://pypi.python.org/pypi/memory_profiler – chinmay rakshit Mar 26 '17 at 14:39
  • But why are you wondering? What possible benefit do you see of the functions? – jonrsharpe Mar 26 '17 at 14:39
  • 2
    I'm wondering because I like to learn and better myself in all aspects. It's knowledge to have, so why not have it? –  Mar 26 '17 at 14:43

2 Answers2

1

If the value never changes, as in there never needs to be any logic to determine the values, simply use variables. By calling a function, it tells the user using the module that some operation needs to run in order to determine the value. If you are just returning a value without doing anything, it's misleading and confusing.

Julien
  • 5,243
  • 4
  • 34
  • 35
  • Fair enough, makes sense. –  Mar 26 '17 at 14:47
  • Is there anything you feel I could add to this answer for you to deem it acceptable? – Julien Mar 26 '17 at 14:57
  • I plan on using it as the answer. I'm just giving it a little bit of time in case someone else comes along with something. Your answer is very straightforward and it answers the question more or less. –  Mar 26 '17 at 15:20
0

For that specific example, I would use neither of them. Red will always be [255,0,0], so why would you let it change, I would use a tuple.

Also, calling functions in Python is relatively slow, so I wouldn't use that for a module, especially when the code becomes complex enough.

But why save those values at all, I think that everyone who will read that module will know how that stuff works: [255,0,0] is red, [200,0,0] is darker, etc. Unless your module uses such amount of trivial values, no, and if you'd like that the people using your module can just make red or red(), they should be able to do that by themselves.

Juan T
  • 1,219
  • 1
  • 10
  • 21
  • The concept goes further than a demonstration of color. Yes, people should know red = (255,0,0). And yes, a tuple would work better in the example than a list. But what if I have something like the Euler–Mascheroni constant, which not everyone might know. I could save it in my module and have it be returned later in my program so I wouldn't have to memorize .57721 or what not. The example is just an example of something simple. Not a definitive replication of what needs to be done in some code somewhere. –  Mar 26 '17 at 15:19
  • In that case I think it's better to just use variables. The thing that I'm saying is that defining superfluous variables, or functions that don't make more than just returning a constant is not something that will improve your code in any way. – Juan T Mar 26 '17 at 15:29