0
def calculate_set(x,y, city, address, geo, etheraddress, email, country_of_origin):
   print(city)
   print(address)
   print(email)
   print(country_of_origin)

Suppose I pass the order of arguments in the wrong way:

calculate_set(30,40, geo = "rowanda", 
                 etheraddress= "xu203942", 
                 address="baker street 304", 
                 city="london", 
                 email = "abc@gmail.com", 
                 country_of_origin= 'uk')

The function will still work fine, whereas in java, it would just print error msg saying the order of args is wrong and it does not recognize it? Is this the purpose of keyword args in python?

ERJAN
  • 23,696
  • 23
  • 72
  • 146

2 Answers2

2

There are many ways to pass arguments to a function. Internally, arguments are passed by assignment. Function arguments are references to shared object referenced by the caller.

Positional parameters: matched left to right

The normal case where arguments are passed by position.

func(a, b)  # Recieved as is left to right

Keywords arguments: matched by argument name

Callers can specify which argument in the function is to receive a value by using the argument’s name in the call.

func(a=10, b=20)  # Keyword argument or named argument -> POSITION DOESN'T MATTER

variable args: get unmatched positional or keyword arguments

Functions can use special arguments to collect arbitrarily many extra arguments (much as the varargs feature in C, Java which supports variable-length argument lists).

func(**var_dict)  # Pass arguments as dictionary 

Defaults: specify values for arguments that aren’t passed

Functions may also specify default values for arguments to receive if the call passes too few values

def func(a=10):
    print(a)

func()  # Prints 10

NOTE

Positional parameters can't be passed after keyword arguments

Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55
1

In Python only positional args (args w/o default values) require right order. Keyword arguments can be passed in any order, but should be passes only after positional args.

trsvchn
  • 8,033
  • 3
  • 23
  • 30