##### When Defining a Function with an Equation as a Default Argument, which Value Is Used?
 When Defining a Function with an Equation as a Default Argument, which Value Is Used? OJGeorge4 Unladen Swallow Posts: 2 Threads: 2 Joined: Apr 2020 Reputation: Apr-09-2020, 07:13 AM I have shortened my function and code to make it easier to follow but it reflects an equivalent situation. I am wondering when you define a function with input arguments having default values that are described as equations, where are the variables in the equations taken from my python? I shall explain with my example below. I want the default value for y in the nested dictionaries to increase the more keys are added to the main dictionary as shown by y in the code below. ```my_dict = dict() default_sizes = [300, 100] def create(text = "Default Text", x = default_sizes[0], y = default_sizes[1] + (10*len(my_dict))): local_dict = dict() local_dict["x_position"] = x local_dict["y_position"] = y print("Internal: ", default_sizes[1] + (10*len(my_dict))) return local_dict print("Before: ", default_sizes[1] + (10*len(my_dict))) my_dict["1"] = create() print("My_dict[\"1\"][\"y_position\"]: ", my_dict["1"]["y_position"]) print("After 1: ", default_sizes[1] + (10*len(my_dict))) my_dict["2"] = create() print("My_dict[\"2\"][\"y_position\"]: ", my_dict["2"]["y_position"]) print("After 2: ", default_sizes[1] + (10*len(my_dict)))```The output of the code is as follows: Before: 100 Internal: 100 My_dict["1"]["y_position"]: 100 After 1: 110 Internal: 110 My_dict["2"]["y_position"]: 100 After 2: 120 So as you can see when the create function is run, the len(my_dict) is calculated using the original length of my_dict above the defined function rather than the new length of my_dict. Inside the function it uses the new my_dict value but in the argument defaults it seems to use the my_dict from above the function. Am I correct? Is there any way to fix this so the argument uses the current variables when it is called? Thank you so much in advance for this head sore, really appreciate any help with this. Reply Posts: 7,567 Threads: 133 Joined: Sep 2016 Reputation: Apr-09-2020, 07:33 AM (This post was last modified: Apr-09-2020, 07:50 AM by buran.) default argument's are evaluated at function definition time. If you want different value, pass it as argument when calling function, not as "new" default argument ```my_dict=dict() DEFAULT_X = 300 DEFAULT_Y = 100 def create(text = "Default Text", x=DEFAULT_X, y=DEFAULT_Y): local_dict = dict() local_dict["x_position"] = x local_dict["y_position"] = y print("Internal: ", y + (10*len(my_dict))) return local_dict print("Before: ", DEFAULT_Y + (10*len(my_dict))) my_dict["1"] = create() print("My_dict[\"1\"][\"y_position\"]: ", my_dict["1"]["y_position"]) print("After 1: ", DEFAULT_Y + (10*len(my_dict))) my_dict["2"] = create(y=DEFAULT_Y + (10*len(my_dict))) print("My_dict[\"2\"][\"y_position\"]: ", my_dict["2"]["y_position"]) print("After 2: ", DEFAULT_Y + (10*len(my_dict)))```Now, I don't fully understand what you are doing, but it may be possible to do it better/cleaner, if for example using namedtuple or custom class. Also you don't use the text param of the function If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply DeaD_EyE Da Bishop Posts: 1,640 Threads: 6 Joined: May 2017 Reputation: Apr-09-2020, 07:42 AM I refactored the code a litte bit. Test it if it works as expected. Look at the differences. Have no time to check it for myself. ```my_dict = {} default_sizes = [300, 100] def create(text="Default Text", x=None, y=None): local_dict = {} if x is None: local_dict["x_position"] = default_sizes[0] else: local_dict["x_position"] = x if y is None: local_dict["y_position"] = default_sizes[1] + len(my_dict) else: local_dict["y_position"] = y return local_dict for idx in range(10): my_dict[idx] = create() print(my_dict)``` My code examples are always for Python >=3.6.0 Almost dead, but too lazy to die: https://sourceserver.info All humans together. We don't need politicians! Reply Posts: 7,567 Threads: 133 Joined: Sep 2016 Reputation: Apr-09-2020, 08:07 AM ```from collections import namedtuple Point=namedtuple('Point', 'x y', defaults=[300, 100]) def create_points(point=Point(), step=10, number=0): current = 0 while True: yield Point(point.x, point.y + current * 10) current += 1 if current == number: break my_dict = {idx:point for idx, point in enumerate(create_points(number=10))} print(my_dict) points = create_points() for i in range(5): print(next(points))`````````Output:{1: Point(x=300, y=100), 2: Point(x=300, y=110), 3: Point(x=300, y=120), 4: Point(x=300, y=130), 5: Point(x=300, y=140), 6: Point(x=300, y=150), 7: Point(x=300, y=160), 8: Point(x=300, y=170), 9: Point(x=300, y=180), 10: Point(x=300, y=190)} Point(x=300, y=100) Point(x=300, y=110) Point(x=300, y=120) Point(x=300, y=130) Point(x=300, y=140)`````` If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply DeaD_EyE Da Bishop Posts: 1,640 Threads: 6 Joined: May 2017 Reputation: Apr-09-2020, 08:48 AM By the way, the `dict` could be a `list`. Basically you use the indices as keys. My code examples are always for Python >=3.6.0 Almost dead, but too lazy to die: https://sourceserver.info All humans together. We don't need politicians! Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Defining a function with input abcd 5 705 Feb-21-2021, 02:34 AM Last Post: NullAdmin Defining an object's argument whose name is stored in a variable arbiel 2 566 Dec-11-2020, 10:19 PM Last Post: arbiel How to use a tuple as an argument of a function zarox 5 750 Nov-14-2020, 08:02 PM Last Post: buran calling a function and argument in an input phillup7 3 699 Oct-25-2020, 02:12 PM Last Post: jefsummers Passing argument from top-level function to embedded function JaneTan 2 580 Oct-15-2020, 03:50 PM Last Post: deanhystad return string from function with one argument jamie_01 2 807 May-28-2020, 11:07 PM Last Post: menator01 how to use getter as argument in function nanok66 3 905 May-13-2020, 09:15 AM Last Post: nanok66 Cant define turtle color with function argument Wrightys99 2 711 Apr-22-2020, 01:43 PM Last Post: Wrightys99 Conjugate Gradient having issues with defining A (function to solve [A]{x} = {b} ) DimosG 1 808 Apr-07-2020, 09:03 PM Last Post: micseydel Function with many arguments, with some default values medatib531 3 936 Mar-14-2020, 02:39 AM Last Post: medatib531

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020