diedro
diedro

Reputation: 613

python function optional arguments and skip or not them

I would like to have a function which is able to compute something if the optional argument are not provide.

My idea is something like this:

def Compute(data,**arg):
        #
        if PAR are not provide then
        PAR=gumbel_r.fit(data)
        endif
        #

        QTR = gumbel_r.ppf(data,*PAR)

        return QTR

Could someone help me in this?

I have been trying to find this option but I am not able.

Thanks in advance, Best

Upvotes: 0

Views: 149

Answers (4)

Sayandip Dutta
Sayandip Dutta

Reputation: 15872

You are using variable named argument provision, not optional argument. However, what you want can be achieved in both ways:

Optional argument:

def Sort(data, PAR=None):
    if PAR is None:
        return sorted(data)
    else:
        return sorted(data, reverse=PAR)

Variable keyword arguments:

def Sort2(data, **PAR):
    if not PAR:
        return sorted(data)
    else:
        return sorted(data, **PAR)

Result:

>>> Sort([1,5,3])
[1, 3, 5]

>>> Sort([1,5,3], True)
[5, 3, 1]

>>> Sort2([1,5,3])
[1, 3, 5]

>>> Sort2([1,5,3], **{'reverse':True})
[5, 3, 1]

Upvotes: 1

DarrylG
DarrylG

Reputation: 17156

Calculating default arguments

def Compute(data, arg = None):
  if not arg:
     # Computes value for optional argument since values not provided
     arg = gumbel_r.fit(data)
        if PAR are not provide then


  return gumbel_r.ppf(data, arg)

Usage

result = Compute(data)      # will compute a value for arg
result = Compute(data, 15)  # will use 15 as value for arg

Upvotes: 0

Lukas Neumann
Lukas Neumann

Reputation: 656

You can set a default value for the argument:

    def say(word="A default value"):
        print(word)

Will result in:

    word() #A default value
    word("hi") #hi

Upvotes: 0

Akshata L
Akshata L

Reputation: 1

you can create two functions, one with param and another with no param with respective code.

Upvotes: 0

Related Questions