caio1982/Amazon-Wishlist — comment on commit 5e5d2d540cfa8dae42313325a4c76f0a367054eb
File:  line 0
By ehabkost:

The problem is str()'ing at all (or generally speaking: doing type conversions at all without need). The fact that your original data came from the web has nothing to do with it; the question is how the callers will use the data you return.

A pragmatic reason is: type conversions are just a source of trouble. So, a good rule of thumb could be: just avoid converting stuff and return the data in the format it is, unless the whole point of the method is to help to make a conversion to help other code.

examples:
- total_expenses() is using floats to make the calculation, so there's no problem at all in returning your float data back, just to avoid the trouble with conversions.
- exception to the rule: the point of prices() is to get the text from the web page and return numbers you can do useful work with, so a conversion from string to a number is helpful to the code that uses it.

A more abstract reason is: you are calculating a sum and you already have a numeric value, and the whole point of your method is to calculate a numeric value.

Returning a more restricted type also helps to make your caller understand exactly how your data will look like, and trust that your method won't do anything unexpected in the future. It's easier to say "this method always return a number" than "this method always return a string that looks like a number". It's like the Unicode stuff for textual data: instead of saying "this method returns text that is valid unicode, encoded in UTF-8", you can simply say "this text return unicode data" and let the caller encode it as str only if necessary.