-1

I want to result using 'str += ' style in the lambda function

example (with error) :

t=lambda text text : [c for c in text str += c.upper()]
t(['h','e','l','l','o'])

I expect to result as :

HELLO

How can i fix above lambda function with state variable like 'str +=' style

if 'str +=' style not possible, please explain in detail why the impossible. Do not just Short-Answer that wrong.

user3736174
  • 333
  • 2
  • 3
  • 16
  • 1
    There are at least two syntax errors in the above code. Please post valid Python. – Daniel Roseman Sep 05 '16 at 09:01
  • BTW, although you _can_ concatenate strings using `+=` it is better to use `.join`, please see [Why is ''.join() faster than += in python?](http://stackoverflow.com/questions/39312099/why-is-join-faster-than-in-python) and the linked pages for details. – PM 2Ring Sep 05 '16 at 09:09
  • Note that assigning a `lambda` expression to a name defeats the entire purpose of `lambda` expressions and is expressly recommended against in PEP-8. – TigerhawkT3 Sep 05 '16 at 09:10
  • i will confirm to lambda function which possible save state like 'str +=' style – user3736174 Sep 05 '16 at 09:11
  • I don't really understand what you mean by that, but assignment statements are not possible in `lambda` functions, as they can only contain expressions, not statements. If you're assigning it to a name, you might as well use a traditional function definition. – TigerhawkT3 Sep 05 '16 at 09:12
  • 1
    Also, don't use `str` as a variable name (even in example code) as that shadows the built-in `str` type. – PM 2Ring Sep 05 '16 at 09:14

1 Answers1

4

I think you mean this:

>>> t = lambda text: ''.join(text).upper()
>>> t(['h','e','l','l','o'])
'HELLO'

Documentation: Lambda Expressions

TerryA
  • 58,805
  • 11
  • 114
  • 143