Python Forum
When Defining a Function with an Equation as a Default Argument, which Value Is Used?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When Defining a Function with an Equation as a Default Argument, which Value Is Used?
#3
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)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: When Defining a Function with an Equation as a Default Argument, which Value Is Used? - by DeaD_EyE - Apr-09-2020, 07:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  mutable argument in function definition akbarza 1 494 Dec-15-2023, 02:00 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,109 Dec-25-2022, 03:00 PM
Last Post: askfriends
  i want to use type= as a function/method keyword argument Skaperen 9 1,910 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  Conjugate Gradient having issues with defining A (function to solve [A]{x} = {b} ) DimosG 2 2,840 Sep-21-2021, 08:32 PM
Last Post: 1968Edwards
  Regex - Pass Flags as a function argument? muzikman 6 3,623 Sep-06-2021, 03:43 PM
Last Post: muzikman
  Defining a function with input abcd 5 3,137 Feb-21-2021, 02:34 AM
Last Post: NullAdmin
  Defining an object's argument whose name is stored in a variable arbiel 2 2,198 Dec-11-2020, 10:19 PM
Last Post: arbiel
  How to use a tuple as an argument of a function zarox 5 3,652 Nov-14-2020, 08:02 PM
Last Post: buran
  calling a function and argument in an input phillup7 3 2,630 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  Passing argument from top-level function to embedded function JaneTan 2 2,274 Oct-15-2020, 03:50 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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