function - Resources on Professional Python Programming -


i'm looking resource me turn python function definitions professional quality functions.

for example, current python definitions have following form:

def foo(arg1,arg2,arg3=some_val,arg4=some_other_val):     """ args """      # create data using args      if arg3 == this_val:         return dataset_1, dataset_2      else :         return dataset_1, dataset_2, dataset_3 

i way return data i'm interested in based on return data ask for.

for example, say:

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

or

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

how make function doesn't need optional argument know want 2 datasets?

this analogous matplotlib constructors 1 can say:

n = plt.hist(data) 

where n histogram 1 can do

n, bins = plt.hist(data) 

and hist function knows return 2 values same input of data (ie no optional arguments stating should return bins).

while may seem clunky , inelegant, unpack 3 variables , use 2 of them if desired:

def foo(arg1, arg2):   return (arg1,arg2,3)  a, b, _ = asd(1,2) print a, b # print "1 2" 

while _ not mean "unused variable" in python in many other languages, many (or perhaps most) recognize such. languages don't offer explicit support it, lua, encourage use of _ placeholder variable due conventions.

use in such way clear _ means though, in python shell contains value of evaluated expression:

>>> 1+2 3 >>> _+3 6 >>>  

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -