1

I am trying to make my code a bit more dynamic and smaller so I am trying to use some arrays, but I need to put a function in a array. I heard of the lambda but the problem is that it doesnt need to return any value but it does need to set a value. this is what I tried but didn't work:

variableArray = [lambda: value = '2' if variable == 1 else None,lambda: value = '3' if variable == 1 else None]
    for k in range(len(variableArray)):
        print(variableArray[k](2))

but I can't set a value with this methode and I wasn't abled to find any other methodes. so are there any other methodes that would work with what I am trying to do?

4
  • secnetix.de/~olli/Python/lambda_functions.hawk Commented Jan 20, 2014 at 20:12
  • that looks really difficult to read. It may be possible to get this to work - but should you do it? I argue no. Commented Jan 20, 2014 at 20:15
  • If the functions are sufficiently unique, you have no other choice but to simply write several function declarations. If there's a pattern to them, it may be possible to automate function construction, but if this is true then one function would probably suffice as well. If you're planning to use those two lambda functions, I'd recommend against it; it'd be much easier to express the same idea with different code. What exactly are you trying to do? Commented Jan 20, 2014 at 20:28
  • Also note that "automate function construction" has its gotchas. For example [(lambda: x+1) for x in range(5)] returns a list of functions, but they all act as if x was 4. Commented Jan 20, 2014 at 20:56

3 Answers 3

3

Python distinguish between expressions and statements (and here compound statements). Expressions can not contain any statements. An assignment is a statement. lambdas are expressions, hence there's no way to use a lambda with that syntax.

However using normal defs wouldn't change much:

def function():
    value = '2' if variable == 1 else None

Here the function is creating a new local variable value, assigning it the its value and then removing it. If you want to modify a non-local variable you must declare it as global:

def function():
    global value
    value = '2' if variable == 1 else None

Note that you can put functions defined with defs into a list:

some_list = [function, #other functions]

However you should try to avoid using global variables as much as you can. You should prefer function parameters and return values to communicate between the function and the rest of the program.

In particular you probably wanted to write functions such as:

variableArray = [lambda arg: '2' if arg == 1 else None, lambda arg: '3' if arg == 1 else None]

You can then do:

for func in variableArray:
    value = func(2)

This will have the same effect as your code.


Note that the lambdas should have a parameter, otherwise calling them with the (2) will raise an error.

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

5 Comments

the problem is though that i dont want to need to add a seperate function for every thing, because it is going to get huge so that is why i wanted to put it in array so it easy to change and such aswell
@user3080469 I believe you should try to explain what is the purpose of the code you posted. Without knowing what the functions represent I don't see how we can help you to find a solution.
btw just a question aswell, what is wrong with globalvariables? does it make the file bigger or make it slower or something liek that?
@user3080469 Global variables introduce shared state. Shared state increases complexity. Complexity increase bugs. With shared state if you have a bug in one of the functions it can affect anything in your program modifying those global variables. Without global variables this kind of problems is greatly reduced. This is especially true if you ever want to do multi-threading and parallel programming where more than one thing might be happening at the same time. See: en.wikipedia.org/wiki/Global_variable and c2.com/cgi/wiki?GlobalVariablesAreBad
If you have a bunch of functions related to some data then maybe you should write a class to handle that piece of state in a coordinated manner.
2

Functions in arrays is possible. But you can't put assignments in lambdas. And your lambda isn't taking any arguments, despite passing 2 into them.

And for k in range(len(variableArray)): is wasteful bad form. Try just:

for k in variableArray:
    print(k(2))

As for the functions, maybe you want this?

variableArray = [lambda x: '2' if x == 1 else None, lambda x: '3' if x == 1 else None]

4 Comments

Then, OP, to set a value, you could do something like var = variableArray[1](2)
yet again i want to be abled to set the variable i want to change in the array because it will be different variables
Can you show us an example of what variables you want to set in your question?
well just a whole bunch of variables, it changes the state of a game i am making basically so i need to change the 'frame', 'action','accelereration','jumptime',etc. and it will probably get a lot bigger so i wanted to try set them in a array
0

It's not entirely clear what you need, but maybe this is closer to what you're after.


If you want to get a list of values from the list of functions, passing a single argument into the function-list, try this:

functions = [lambda x: '2' if x == 1 else None, lambda x: '3' if x == 1 else None]
values = [function(2) for function in functions]

Comments

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.