F
F
fantom_ask2020-04-28 13:44:51
Python
fantom_ask, 2020-04-28 13:44:51

How can I pass nothing to the function so that the default value would work?

I'm just learning python so I don't know if it's possible to pass an empty to a function so that the default value would work. After all, I have two functions where one passes a value to the other, and in order not to write the default value twice, is it possible to write an empty one.

Is there such a value in python?

Using if else is not suitable because if there are several variables, then you have to write all combinations.

def test_1(arr_1=[1,232,1134,134,1113,442,23,1134,313,4,5232,533,32], arr_2=[393,399,23009,11999,309,399,9]):
  #Просто выводит либо значения по умолчанию либо то что в него передали неважно что
  print("{}-{}".format(arr_1,arr_2))
  
def test_2(val_1=None,val_2=None):
  #сокращенно
  test_1(val_1,val_2)
  
  #C использованием if else
  if val_1== None and val_2==None:
    test_1()
  elif val_1== None and not val_2==None:
    test_1(arr_2=val_2)
  elif val_2== None and not val_1==None:
    test_1(val_1)
    
  # Дальше исполняет что то
  
test_1()
test_2()
test_2('f')
test_2('f','D')
test_2(val_2='D')


The test_1 function cannot be edited in any way, as it can be used in different parts of the code.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2020-04-28
@fantom_ask

*args, **kwargs

def test_1(val_1=(1, 232, 1134), val_2=(393, 399, 23009)):
    print("{}-{}".format(val_1, val_2))


def test_2(*args, **kwargs):
    test_1(*args, **kwargs)


test_2()
test_2('f')
test_2('f', 'D')
test_2(val_2='D')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question