1

in threading.timer you pass the name of a function (example below). Is it possible to replace this with the function body so I dont have to bother defining another trivial function

def PulseLed(self):
    GPIO.output(26, GPIO.HIGH)
    self.ledTimer = threading.Timer(1, self.TurnOffLed)
    self.ledTimer.start()

def TurnOffLed(self):
    GPIO.output(26, GPIO.LOW)
1
  • You are not passing name in this example, you are passing a method object. Why not try to simply replace it with anonymous function? Commented Sep 26, 2013 at 0:01

4 Answers 4

6

Python's syntax does not allow including statements in argument lists. Often you can use a lambda, though, like:

threading.Timer(1, lambda: GPID.output(26, GPIO.LOW))

Overused, this can lead to unreadable code. You know it's "overused" if you start to stuff multiple x if b else y clauses into your lambda ;-)

Sign up to request clarification or add additional context in comments.

Comments

2
self.ledTimer = threading.Timer(1, lambda: GPIO.output(26, GPIO.LOW))

Comments

2

I'm always partial to partial:

from functools import partial

def PulseLed(self):
    GPIO.output(26, GPIO.HIGH)
    self.ledTimer = threading.Timer(1, partial(GPIO.output, 26, GPIO.LOW))
    self.ledTimer.start()

2 Comments

I think using functools.partial is an overkill (simple lambda would do), but it still works. Upvoting.
When all you want to do is bind some static positional arguments, it's hard to see how partial is overkill, or underkill. When you start doing convoluted things with it, or trying to combine it with a compose function, that's when you realize you're not actually writing Python anymore… (Well, that's when I realize; maybe some people realize it faster.)
2

I like the "trivial function" in many cases; however, I would write it similar to following. I find that using local functions - just as with using local variables - can add "self documentation" to code.

def PulseLed(self):
    def tunOffLed():
        GPIO.output(26, GPIO.LOW)
    GPIO.output(26, GPIO.HIGH)
    self.ledTimer = threading.Timer(1, turnOffLed)
    self.ledTimer.start()

(Perhaps even put the other GPIO call in a turnOnLed function.)

2 Comments

If we had PEP 403 you could even make it statement-local instead of function-local. Which really isn't any more readable in this case, or in most cases, which is why Nick Coghlan deferred the PEP, but some day someone will find a killer use case for it…
@abarnert I can't say I like the syntax proposed, but it's a nice read (thanks for the link!). Some languages allow similar constructs already, showing that there is merit with some code/cases (question is then if it fits the Python Way well). And, for some reason, such a construct always appears whenever I fiddle around with syntax design for never-materializing pet languages ..

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.