0

I am a complete newbie in python.

I start doing lambda functions and they end up a bit longer than my initial goal:

Can I split it in different lines for better readability?, like this:

parts.map(lambda p: (p[0]\
                     ,p[1]\ 
                     ,int(p[1].split("-")[0])\
                     ,int(p[1].split("-")[1])\
                     ,p[2]\
                     ,float(p[3])\
                     ,p[4]))

or it defeats the purpose of using a lambda function?

I feel when I write it is ok to use lambda function in one line, is quick and good, but when I check again my code later I feel is not legible all of it in one line...

4
  • 2
    write a function with all that stuff inside, then call the function Commented Sep 17, 2019 at 7:51
  • FYI In Python, list comprehensions are generally preferred over uses of .map Commented Sep 17, 2019 at 7:52
  • If it runs, then it is allowed, I'd say. Question is; would you want to? I think lambda's should be reserved for quick, on the fly computations/alterations, whatever. This feels like something that just would be better of as a function you call on your object. Commented Sep 17, 2019 at 7:54
  • if only readability is your problem then I suggest using black to format your code. Commented Sep 17, 2019 at 8:00

3 Answers 3

2

If it's not clearly readable as a simple one-liner, then it's not a good candidate for a lambda. Remember that the lambda statement is just syntactic sugar, technically it IS a function:

>>> def foo(): pass
... 
>>> bar = lambda: None
>>> 
>>> type(foo)
<class 'function'>
>>> type(bar)
<class 'function'>
>>> 

So yes, in your example it does definitly "defeat the purpose of using a lambda function". As far as I'm concerned, if I had to maintain this code, I'd rather find something like:

def prepare(p):
   p1a, p1b = (int(x) for x in p[1].split("-"))
   p3f = float(p3)
   return p[0], p[1], p1a, p1b, p[2], p3f, p[4]


whatever = [prepare(part) for part in parts]
Sign up to request clarification or add additional context in comments.

Comments

1

If you are interested in style and readability I can't recommend the PEP8 style guide enough. Overall, it explains the best practices to write readable Python. It will in particular give you advice on where to put commas when you start a new line, when to use parenthesis and how and when to write to a new line.

On lambda functions in particular it states:

Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier.

Yes:

def f(x): return 2*x

No:

f = lambda x: 2*x

In your case, I would use a function instead.

1 Comment

Well, this is not exactly relevant to OP question as it treats different misuse of lambda...
1

You could define a normal function and just use it in the map function if it gets too long.

def foo(p):
    '''your code'''

result = list(map(foo, your_list)) # the list wrapper to convert map object to a list

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.