I've heard that python functions are objects, similar to lists or dictionaries, etc. However, what would be a similar way of performing this type of action with a function?
# Assigning empty list to 'a' a = list() # Assigning empty function to 'a' a = lambda: pass # ??? How would you do this? Further, is it necessary or proper? Here is the sense in which I would like to use it for better context:
I have a QListWidget for selecting items which are associated with keys in a dictionary. The values in this dictionary are also dictionaries, which hold certain properties of the items, which I can add. These certain properties are stored as keys, and the values in them are initialized or updated by calling different functions. So, I'm storing a variable in the window which gets updated when a button is pressed to tell this script which property to update.
As you can see, I would like to store the function to map to the data using the correct function based on the situation.
# Get selection from the list name = selected_item # Initialize an empty function f = lambda: pass # Use property that is being added now, which was updated by the specific button that was pushed property_list = items[name][self.property_currently_being_added] if self.property_currently_being_added == "prop1": f = make_property1() elif self.property_currently_being_added == "prop2": f = make_property2() elif self.property_currently_being_added == "prop3": f = make_property3() elif self.property_currently_being_added == "prop4": f = make_property4() # map the certain function to the data which was retrieved earlier added_property = map(f, data) property_list.append(added_property)
else: raise ValueErroror etc. It doesn't seem like you'd want this function to continue ifself.property_currently_being_added == Noneor what-have-you.def f(): pass, with the same effect. The only reasons to ever uselambdaare (a) you don't want to give the function a name (doesn't apply here; you immediately assign it tof); (b) you need to define it in an expression, not a statement (doesn't apply here; you use it directly in an assignment statement); or (c) you're taking a class with a professor who hates Python and would rather be teaching Scheme (presumably doesn't apply here).c = types.CodeType(0, 0, 0, 0, 0, b'', (), (), (), '<main>', 'f', 0, b''), thenf = types.FunctionType(c, {}). Of course all this function does is raise aSystemErrorwhen called… but how useful do you expect an empty function to be? :)