Skip to content

Instantly share code, notes, and snippets.

@Coldsp33d
Created June 11, 2018 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Coldsp33d/8cbda89adcd0e23c60133592afce6bbc to your computer and use it in GitHub Desktop.
Save Coldsp33d/8cbda89adcd0e23c60133592afce6bbc to your computer and use it in GitHub Desktop.

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++.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment