[Python-ideas] Inline Functions - idea

Allan Clark allan.clark at gmail.com
Wed Feb 5 15:44:36 CET 2014


Why not just use 'or' in your conditional?

# First without an inline function
def main():
    file = open('file.txt')
    counter = 0
    while True:
        counter += 1
        frontL, frontR, backL, backR = getWheelSpeeds()
        if ( counter > 100 or # Log at least every 10 seconds
             abs(frontR-backR) > 1 or abs(frontL-backL) > 1 or  # Also log
if the wheels are slipping
             average([frontL, frontR, backL, backR]) > 60): # Also log if
we're going really fast
            slipL = abs(frontL - backL)
            slipR = abs(frontR - backR)
            file.write('Speeds: ('+frontL+', '+frontR+', '+backL+',
'+backR+'), \n Slip: '+slipL+', '+slipR)
            counter = 0
        time.sleep(.1)



On 5 February 2014 14:32, Alex Rodrigues <lemiant at hotmail.com> wrote:

> Hi everyone,
>
> This is my first time on the Python mailing lists. I've been learning a
> lot about how python is run recently and today I thought I ran across an
> idea which might create in interesting discussion.
> Today I was writing a piece of software where I had a block of code that
> would take a bunch of local variables, apply some transformations to them
> and then output them as a a string to a log. Then I realized that I
> actually wanted to reuse this code in multiple locations - there were
> multiple cases where I might need to do this. My natural inclination was to
> write a function in order to maintain DRY programming. This was
> prohibitively challenging, however, since the code itself interacted with
> lots of variables in the namespace. The number of arguments to the function
> would have to be very large and possibly would change on a fairly regular
> basis.
> This seems like a fairly common problem in programming, having a piece of
> code which is both reused and heavily integrated with the namespace making
> it necessary to use copy-paste. As a solution to this I propose the idea
> of an inline function. An inline function would run in it's parent's
> namespace instead of creating a new one. This would allow you to avoid
> passing back and forth tons of values while still maintaining DRY code. It
> might look something like this if implemented in a log for a traction
> control system:
>
> # First without an inline function
> def main():
>     file = open('file.txt')
>     counter = 0
>     while True:
>         counter += 1
>         frontL, frontR, backL, backR = getWheelSpeeds()
>         if counter > 100: # Log at least every 10 seconds
>             slipL = abs(frontL - backL)
>             slipR = abs(frontR - backR)
>             file.write('Speeds: ('+frontL+', '+frontR+', '+backL+',
> '+backR+'), \n Slip: '+slipL+', '+slipR)
>             counter = 0
>         elif abs(frontR-backR) > 1 or abs(frontL-backL) > 1: # Also log
> if the wheels are slipping
>             slipL = abs(frontL - backL)
>             slipR = abs(frontR - backR)
>             file.write('Speeds: ('+frontL+', '+frontR+', '+backL+',
> '+backR+'), \n Slip: '+slipL+', '+slipR)
>             counter = 0
>         elif average([frontL, frontR, backL, backR]) > 60: # Also log if
> we're going really fast
>             slipL = abs(frontL - backL)
>             slipR = abs(frontR - backR)
>             file.write('Speeds: ('+frontL+', '+frontR+', '+backL+',
> '+backR+'), \n Slip: '+slipL+', '+slipR)
>             counter = 0
>         time.sleep(.1)
>
>
> # And now with an inline function
> def main():
>     file = open('file.txt')
>     counter = 0
>     while True:
>         counter += 1
>         frontL, frontR, backL, backR = getWheelSpeeds()
>         if counter > 100: # Log every 10 seconds no matter what
>             saveLine()
>         elif abs(frontR-backR) > 1 or abs(frontL-backL) > 1: # Also log
> if the wheels are slipping
>             saveLine()
>         elif average([frontL, frontR, backL, backR]) > 60: # Also log if
> we're going really fast
>             saveLine()
>         time.sleep(.1)
>
> inline def saveLine():
>     slipL = abs(frontL - backL)
>     slipR = abs(frontR - backR)
>     file.write('Speeds: ('+frontL+', '+frontR+', '+backL+', '+backR+'),
> \n Slip: '+slipL+', '+slipR)
>     counter = 0
>
> What do you think?
>
> - Alex
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140205/0bf032a0/attachment-0001.html>


More information about the Python-ideas mailing list