-3

I'm looking for a resource that will help me turn my python function definitions into professional quality functions.

For example, my current python definitions have the following form:

def foo(arg1,arg2,arg3=some_val,arg4=some_other_val):
    """ Do something with the args """

    # Create data using args

    if arg3 == this_val:
        return dataset_1, dataset_2

    else :
        return dataset_1, dataset_2, dataset_3

I would like for a way to return the data I'm interested in based on what return data I ask for.

For example, I could say:

ds_1, ds_2, ds_3 = foo(arg1,arg2)

or

ds_1, ds_2 = foo(arg1,arg2,arg3=only_two_datasets_bool)

How do I make it so the function doesn't need the optional argument to know I only want two datasets?

This is analogous to say matplotlib constructors where one can say:

n = plt.hist(data)

where n is the histogram but one can also do

n, bins = plt.hist(data)

and the hist function knows to return those two values with the same input of data (ie no optional arguments stating it should return bins).

alvarezcl
  • 579
  • 6
  • 22
  • 1
    I think you'd better narrow your question a bit. This is pretty broad and will likely get closed ... For args and kwargs, [this has been asked before](http://stackoverflow.com/q/3394835/748858). As far as a function sometimes magically returning 1 thing and sometimes 2 things with *the same input* -- that's not possible. You can make a function that returns a different number of things based on what input you get (which is what I `pyplot.hist` does IIRC) – mgilson Apr 29 '15 at 06:01
  • @mgilson It is possible...with random module :) – Shashank Apr 29 '15 at 06:08
  • @Shashank Yes, it is possible to write a python function that yields different output with the same input (e.g. by reading global state, I/O, etc.). But if mgilson is thinking of a function in a purely mathematical sense (or even like a haskell function), then no, it may not yield different outputs for one set of input. I find code written in this way (where functions are a 1-to-1 mapping) much more maintainable and debuggable than the converse. – EvenLisle Apr 29 '15 at 06:11
  • Right, I just want to know if there are resources showing you what `pyplot.hist` does, in that case. – alvarezcl Apr 29 '15 at 06:26
  • And if not, what's the best way to professionally program a function in python. – alvarezcl Apr 29 '15 at 06:28
  • 1
    @alvarezcl you can read [the source code](https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/pyplot.py#L2878) for `pyplot.hist`. As for *"professionally"*, what do you mean? Requests for off-site resources are **explicitly off-topic**. – jonrsharpe Apr 29 '15 at 06:34

1 Answers1

2

While it may seem clunky and inelegant, you could unpack three variables and only use two of them if desired:

def foo(arg1, arg2):
  return (arg1,arg2,3)

a, b, _ = asd(1,2)
print a, b # will print "1 2"

While the _ does not mean "unused variable" in python as it does in many other languages, many (or perhaps most) will recognize it as such. Even languages who don't offer explicit support for it, like Lua, encourage the use of _ as a placeholder variable due to conventions.

Use it in such a way that it is clear what the _ means though, as it in python shell contains the value of the previously evaluated expression:

>>> 1+2
3
>>> _+3
6
>>> 
EvenLisle
  • 4,672
  • 3
  • 24
  • 47