How do I append a lambda to a list in python?

Kei

I am trying to make a program that creates a list of lambda functions of the format y=mx+b, where 'm' and 'b' are predetermined values

My overall goal is to implement a function that

  • Takes a picture
  • Finds the lines on it
  • Extends them across the whole picture in a solid colour

Basically, something like a Hough transforms if you know what that is.

Once I have the lines for a specific image, I can create a lambda function to represent the slope of the line and where it begins. I'm having an issue not being able to append a lambda function to the list.

I have tried this :

if __name__ == "__main__":
  nums = []
  for i in range(10):
    j = lambda x: x + i
    nums.append(j)
  for i in nums:
    print(i(1))

Here is the error I'm getting :

Traceback (most recent call last):
  File "C:/Users/me/.PyCharmCE2018.3/config/scratches/scratch_3.py", line 7, in <module>
    print(i(1))
  File "C:/Users/me/.PyCharmCE2018.3/config/scratches/scratch_3.py", line 4, in <lambda>
    j = (lambda x: x + i)
TypeError: unsupported operand type(s) for +: 'int' and 'function'
Tom Karzes

The problem is that the lambdas you create are referring to the current value of i in the active stack frame. When you later reuse i for the second for loop, it is bound to the lambdas in your list. When invoked as i(1), the lambdas are trying to evaluate 1 + i where i is the lambda, so of course you get an error.

Probably what you want is to freeze the value of i at the point at which the lambda is created. You can do this by replacing:

j = lambda x: x + i

with:

j = (lambda y: lambda x: x + y)(i)

This effectively captures the current value of i by binding it to a lambda variable, then immediately applying that lambda, after which the binding remains fixed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I append a string to a Path in Python?

How do I append a list in python using a for loop and replace function?

How do I convert an array of strings into append-able list?

How do I append a Python object attribute (list)

How do I append a list with sublists inside of it?

How do I import a Python lambda layer?

How do I append the value of a variable (not the reference) into a list in Python 3.x?

Append to YAML list - how do I only 'append' the value?

How do I append a list recursively in common lisp?

How do I append dataframes in a list?

how do i append two nested list into single nested list in python

How do I append columns from a list of data frames?

How do I copy a list of lists and append it to a new list in python?

How do I append to the correct deeply-nested list?

How do I append a list to an empty numpy array

How do i append a list with new data?

Python - How do I append numbers inside the list and

How do I append to a list once in a double-for loop?

How do I append data to the list in loop?

How do I append a variable from a function into a list while looping code in Python

How do I append a matching element from a list to another list?

How do I append a list to an existing dictionary in python

How do I append to list only after a certain value in another python list?

How do I append text to a file with python?

how do I append something to a list after an element not at the end of the list

Python how do I append to every string in list using lambda function?

How do I append a repeating list to a dataframe?

How to I append elements to a list of lists in python

How do I append the index of a string to a list using extend() in python?