How to implement a counter using a lambda?

Multi tool use
Multi tool use


How to implement a counter using a lambda?



Can I implement a counter using a lambda function in python or some expression more pythonic?


lambda



Here is my code:


counter = 0
if 0 < arrival_time:
counter += 1
else:
pass



this is what I tried:


count_late = lambda x, arrival_time: x+=1 if 0 < arrival_time else pass
counter_late(counter, arrival_time)



The error:
enter image description here



What am I missing or how can I improve this code?





One last thing: When people talk about "a counter using a function", they're often Lisp types looking for something like this code. But if that's what you were aiming for, see the second version directly below it for a more Pythonic solution. (Instances and closures are dual, but that doesn't mean there's no reason to ever prefer one over the other—it's almost always more readable one way or the other.)
– abarnert
Jul 3 at 0:30





Just a small improvement I suggest for your code: you don't need an else statement for every if statement. Removing the else: pass won't affect how your code runs. It saves space and, on some occasions, headaches.
– Joel
Jul 3 at 0:33


else


if


else: pass





@abarnert and is it better to use a class when I am dealing with counters? or just initiate a variable counter and the loop? thanks for the code
– may
Jul 3 at 1:29






@may It depends entirely on what you're doing. If you want a counter that encapsulates some state and provides opaque methods to updaet it, like the example I gave in the link above, then you want a class. If you just want to keep adding to an int, then you probably want either a pure function that returns a new value, or a function that updates a global variable; no need for a class either way. Without seeing the rest of your design, it's hard to say which one you want.
– abarnert
Jul 3 at 1:34





Any particular reason you want to do it with a lambda?
– martineau
Jul 3 at 2:33


lambda




2 Answers
2



Both x+=1 and pass are statements, but lambda is an expression, and you can't put statements inside an expression.


x+=1


pass


lambda



But that's fine.



lambda and def both just create a function, in the same way, but lambda is more limited.


lambda


def


lambda



If you need to create a function in the middle of an expression, you have to use lambda—but that's not the case here, since you're creating it just to use in an assignment statement.


lambda



If there's no good name for a function, you may want to use lambda, but that's not the case here either, because you're immediately giving it a name.


lambda



Some people (mostly those who've spent too much time with Lisp- or ML-family functional languages) also like to use lambda to make it clear that they're writing a "pure function", one that has no side effects and returns a value that depends only on the value of its parameters. But that's not the case here either. (If you changed it to lambda x, arrival_time: x+1 if 0 < arrival_time else x, that would be a good example of a pure function. You'd then call it with, e.g., x = count_late(x).)


lambda


lambda x, arrival_time: x+1 if 0 < arrival_time else x


x = count_late(x)



So, there's absolutely no reason to use lambda here in the first place. Just use def:


lambda


def


def count_late(x, arrival_time):
if 0 < arrival_time:
x += 1



However, it's worth noting that, while this is now valid syntax, it isn't going to do any good.



Numbers are immutable; there's no way to change the number 2 into the number 3, because that would break all of physics. When you write x += 1, that just makes the local variable x into a name for the number 3 instead of a name for the number 2. If you call it with count_late(spam, 5) it's not going to change what spam means, just as if you call it with count_late(2*3, 5) it's not going to change what 2*3 means.


2


3


x += 1


x


3


2


count_late(spam, 5)


spam


count_late(2*3, 5)


2*3



So, you probably wanted to:


return


lambda


def


self.x


x





thanks for the explanation as I am a newbie in python, I found it hard to improve the code with less code using python's features. I believe your explanation will be a reference for me, thank you very much for your answer.
– may
Jul 3 at 1:32



if you have a function counter_late() :


**
#the 'counter' is in counter_late() or global
counter_late((lambda arrival_time: counter+1 if 0 < arrival_time else PASS), 10)



**



else :


**
counter = 0 #global variable

counter = (lambda arrival_time: counter+1 if 0 < arrival_time else PASS)(10)

#check the value of counter
print('conter=',counter)
**



10 is a variable what you want for a value of arrival_time.



if you get a syntax err.



PASS is change other value, you want to a number or condition.



for eg)



counter = (lambda arrival_time: counter+1 if 0 < arrival_time else 1)(0)





this is also an interesting way to see lambda function. The other answer was more aligned but I thank you for sharing this knowledge with me
– may
Jul 3 at 1:33





What is PASS supposed to be here? As written it just turns the SyntaxError into a NameError. The only value I can think of that really makes sense is counter, but just doing PASS = counter isn't going to help anything, because after the first time you do counter = …, that updates counter to the new value but leaves PASS as the old one.
– abarnert
Jul 3 at 1:36


PASS


SyntaxError


NameError


counter


PASS = counter


counter = …


counter


PASS





You're right. I miss that and modified. PASS was used in the needs of the questioner.
– clem
Jul 3 at 1:45







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

uRf3QwfqhNADw5uA7r0
q9Lhue8Dq96vrWWo

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications