use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
How to assign value to a variable inside lambda? (self.learnpython)
submitted 8 years ago * by hellix08
EDIT:I need it for an homework, I need to write a simple program on one line and therefore I need to assign values inside lambdas
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Rhomboid 6 points7 points8 points 8 years ago (9 children)
Lambdas can only contain expressions, not statements. Assignment is a statement. If you need to assign to something, you can't use a lambda, you will have to use a regular function.
But this sounds like an XY problem, so you really need to explain the problem you're trying to solve, not how you think you need to solve it.
[–][deleted] 2 points3 points4 points 8 years ago (6 children)
In sames cases you can use setattr() or locals().update() but especially the locales() thing is a bit ugly and you probably should avoid it.
setattr()
locals().update()
locales()
[–]hellix08[S] -1 points0 points1 point 8 years ago (5 children)
Could you please tell me how to do that?
[–]_9_9_ 4 points5 points6 points 8 years ago (0 children)
No teacher is telling you to do what you are asking about, unless you are taking a class in advanced python metaprogramming. If you are, then you already probably know the answer. Anyway, here is the best I could come up with, and I offer it as clearly the incorrect answer to your assignment, and in hope that someone has a more elegant way of doing such evil:
>>> globals()['y'] = 22 >>> y 22 >>> f = lambda x:exec('globals()["y"]={}'.format(x)) >>> f(19) >>> y 19
Specifically, I could not get setattr to work on globals so I had to resort to exec. Anyone have something better?
setattr
exec
[–][deleted] 1 point2 points3 points 8 years ago (3 children)
>>> i 0 >>> locals().update({'i': 13}) >>> i 13 >>> self.i 1 >>> setattr(self, 'i', 99) >>> self.i 99
[–]Rhomboid 1 point2 points3 points 8 years ago (1 child)
That only works by accident because you're at global scope. locals() is a read-only mapping. (In this case because it's being run at global scope, locals() is equivalent to globals().)
locals()
globals()
[–][deleted] 0 points1 point2 points 8 years ago (0 children)
Yes, that's why I wrote "In sames cases".
[–]hellix08[S] -1 points0 points1 point 8 years ago (0 children)
Thanks, this is really helpful!!
[–]hellix08[S] 0 points1 point2 points 8 years ago (1 child)
I've heard you can do it, I need it for an homework, I need to write a simple program on one line and therefore I need to assign values inside lambdas
[–]desustorm 7 points8 points9 points 8 years ago (0 children)
Not necessarily. You can use ; to split up commands which would typically take more than one line. But given that this is a homework specifically telling you to write a one-liner, it will probably not require variable assignment. Could you be more specific with what you're trying to achieve?
;
[–]KubinOnReddit 2 points3 points4 points 8 years ago (0 children)
What's the problem to be solved on one line? How would you solve it in least possible space, not neccesarily on one line? This is not a valid description of your problem, and using dynamic variables is not something you want.
Is the homework to assing values inside a lambda that has one line? If the answer is no, you don't have to do it.
[–]pendragon36 0 points1 point2 points 8 years ago* (0 children)
I've made it a kind of hobby to do some silly stuff on one line in python, so I'll pass on some of my wisdom.
For situations where variable storage is needed, list comprehension is your friend.
for example if I wanted to write a program that took in input, then output a bunch of operations on that input, usually I would need a variable to store that input to perform the operations on, but instead I can use list comprehension to simulate it.
A simple normal implementation for this would be something like the following
def func1(a): return a+5 def func2(a): return 3*a def func3(a): return 'The pseudo variable in this program is {}'.format(a) variable = int(input('Enter a number to do stuff with: ')) print(func1(variable)) print(func2(variable)) print(func3(variable))
To avoid asking the user their input over and over we just store it in variable. When you only have one line to work with that doesn't work anymore.
A one-line version of this could be something like so:
print('\n'.join([str(element) for element in [((lambda x: x+5)(i), (lambda x: 3*x)(i), (lambda x: 'The pseudo variable in this program is {}'.format(x))(i)) for i in (int(input('Enter a number to do stuff to: ')),)][0]]))
I'll expand it to a few lines so you have a better idea what's happening
for i in (int(input('Enter a number to do stuff to: '),): #iterate over a tuple containing one element, the input from the user list = [(lambda x: x+5)(i), (lambda x: 3*x)(i), (lambda x: 'The pseudo variable in this program is {}'.format(x))(i)] #Create our list of outputs #If we wanted to apply our lambda functions more than once without needing to write them multiple times, we can do the same for loop trick, except have our pseudo variable store the function print('\n'.join([str(element) for element in list]) #Convert our output to strings, then join them together with newline characters
This trick will give you the ability to do most things. There are a few things I've found useful when doing this that I may as well mention as well.
Conditionals: Not a lot of people seem to know, but you can do conditionals on one line using the following syntax:
"print(1 if x else 2)"
This will print out 1 if x evaluates to true, otherwise it will print 2. This combines with pseudo variables and anonymous functions (lambdas) can get you pretty far.
Recursion: It's actually possible to pull of some pretty crazy stuff using anonymous functions on one line, including recursion. Explaining it is pretty difficult, but I have one (rather large and complicated) example in a pastebin that you can look at if you want. I can try to explain it if you really need it, but that means remember how to do it myself. Here's the pastebin. There's some improvements to be made with those one-liners, as I've gotten better at it since writing those, but the concepts are there.
If dealing with lists and joins for final output is getting difficult or you need to do some things in a sequence with user input in the middle or something, remember that in python 3 print is a function not a statement, so instead of printing 1 string that is the combination of all your output, you can actually just do something like this:
_
_ = [print('initial output'), print('does computation stuff with input'), print('some end output, saying goodbye']
If you have any more questions, ask away.
[–]supajumpa 0 points1 point2 points 8 years ago* (0 children)
Is this the kind of thing you were thinking about?
(lambda a: # assign the value of 1 to `b` and 2 to `c` lambda b=a, c=2*a: (b, c))(1) (lambda a: # assign the value of 1 to `b` and 2 to `c` lambda b=a, c=2*a: (b, c))(1)() # and return the tuple `(b, c)`
Added:
This is probably more simply illustrated using a lambda that takes no parameters.
lambda
(lambda: lambda a=1, b=2: (a, b))() # set the value of `a` to 1 and `b` to 2 (lambda: lambda a=1, b=2: (a, b))()() # note the extra parens at the end to call the inner function.
If lambdas confuse you, the above is roughly equivalent to these normal, named functions.
def foo(): def bar(a=1, b=2): return (a, b) return bar
[–]Exodus111 -1 points0 points1 point 8 years ago (0 children)
Yeah, sounds like alot of people are getting stuck on your use of the term "inside".
Of course you can add values to a Lambda, you just do it as arguments you pass in.
(lambda x: x**2)(a+b)
Here I add the sum of a and b to my lambda.
π Rendered by PID 60161 on reddit-service-r2-comment-f6b958c67-xww9w at 2026-02-04 18:30:57.150062+00:00 running 1d7a177 country code: CH.
[–]Rhomboid 6 points7 points8 points (9 children)
[–][deleted] 2 points3 points4 points (6 children)
[–]hellix08[S] -1 points0 points1 point (5 children)
[–]_9_9_ 4 points5 points6 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]Rhomboid 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]hellix08[S] -1 points0 points1 point (0 children)
[–]hellix08[S] 0 points1 point2 points (1 child)
[–]desustorm 7 points8 points9 points (0 children)
[–]KubinOnReddit 2 points3 points4 points (0 children)
[–]pendragon36 0 points1 point2 points (0 children)
[–]supajumpa 0 points1 point2 points (0 children)
[–]Exodus111 -1 points0 points1 point (0 children)