Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python style question

Is it better to pass in a zipped list of pairs, or two lists that must agree in length?

With two lists, I have to assert their lengths agree, but at least the parameters are named.

I am asking about the prevailing style. Are there any examples of choosing one over the other from major libraries? For example, I guess matplotlib passes in two arrays.

like image 999
Neil G Avatar asked May 09 '26 08:05

Neil G


2 Answers

If your function is going to effectively zip the lists anyway, I would probably prefer to zip the lists in the caller. Then the caller can decide what to do if the lists are not the same length. Also, if your pairs are logically related then the caller might already have the data as a list of pairs, and it would be inconvenient to have to unzip it first.

like image 150
Greg Hewgill Avatar answered May 10 '26 20:05

Greg Hewgill


In my opinion it depends on the semantics of the routine whether these checks must be done in the caller or in the callee.

like image 40
Andrea Spadaccini Avatar answered May 10 '26 21:05

Andrea Spadaccini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!