0

there are (say) two functions, norm and uniform, both of which take the arguments scale and loc. I would like to have a function called pick_a_distribution, which selects which distribution to pick and then assigns the appropriate arguments and does some other stuff. But I also want the user to be able to define (say) a value for one argument and pass only that, rather having to define the whole list of arguments.

I have managed to write some code that works, if I pass the full list of arguments (and therefore I need to know what the default values are, for the ones I do not really want to pass).

Is there a way to be more efficient (in the above example, would there be a way to only pass the 'loc' argument)?

below is some code that works . . .

from scipy.stats import norm, genlogistic, uniform, expon
import math

distribution = {'1': 'normal', '2': 'uniform', '3': 'genlogistic', '4': 'exponential', '5': 'error'}
def mydistr(distr,numofsims, myargument):
    switcher = {
        '1': norm(*myargument),
        '2': uniform(*myargument),
        '3': genlogistic(*myargument),
        '4': expon(*myargument)
    }
    myobs = switcher.get(distr, 'Error in getting distribution')
    return myobs.rvs(numofsims)


print(mydistr( myargument = [2,80], numofsims = 100000, distr = '2' ).mean())

and some that doesnt. . . (same imports as before)

    def mydistr(distr,numofsims, myargument):
        switcher = {
            '1': norm(*myargument),
            '2': uniform(**myargument),
            '3': genlogistic(*myargument),
            '4': expon(*myargument)
        }
        myobs = switcher.get(distr, 'Error in getting distribution')
        return myobs.rvs(numofsims)

    print(mydistr( myargument = {loc:80}, numofsims = 100000, distr = '2' ).mean())

I've also tried to have the following for myargument in the final call: = ({loc:80}), = (loc,80) and tried 'loc' as well;

I get that name 'loc' is not defined, and when i put it in '' I get some comparison errors.

Any comments much appreciated, thank you in advance :)

nikopkat
  • 3
  • 2
  • 1
    Does this answer your question? [What does \*\* (double star/asterisk) and \* (star/asterisk) do for parameters?](https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters) – Harun Yilmaz Mar 03 '20 at 10:56
  • 1
    Can you clarify your goal? This seems like an [XY problem](http://xyproblem.info). Since client code has to select the distribution anyways, and has to pass all its parameters as well – why not take the fully constructed distribution? Note that your switching code is flawed: It creates all distributions (even those not used) and defers errors by hiding lookup errors. Your second switching code expects ``myargument`` to be both keyword (via ``**``) and positional (via ``*``). – MisterMiyagi Mar 03 '20 at 11:04
  • I am trying to make the code more efficient, by only passing non-default values. This is an example only, I may have many other functions to which I'd like to apply this. to MisterMiyagi : the second switching code was written in haste - the intention is that all would be either * or **. To Harun: Thanks for your suggestions - one thing I picked up is that I should put the myargument in the end of the list rather than the start - but still didnt work. I know that uniform(loc=80) would work, but not sure how to get the same result by passing the argument through another function. – nikopkat Mar 03 '20 at 11:23

0 Answers0