I was wondering if it is a good approach to define a one-line method for the sake of making the whole program more readable?
If you're assigning a function to a shorter name, yes.
short_name = module.submodule.xtrelemly_long_name_here
Now every call to module.submodule.xtrelemly_long_name_here
can be substituted with short_name
, for readability.
However, defining one line functions can be done like this:
f = lambda x: ...
But lambda
functions are anonymous, and naming lambdas is redundant when you could just do
def f(x):
...
return something
By the way,
def f(x): return something
Does not count as a single lined function.
And does that approach change the efficiency of my program?
When you're dealing with python, performance is pretty much moot outside the context of our numerical libraries. If you're looking for a language where microoptimizations are going to make a meaningful difference, I'd recommend switching to C or C++.