Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python convert style: inside or out of function?

Tags:

python

styles

I have a function that expects to operate on a numeric type. I am reading the numbers to operate on from a file, so when I read them they are strings, not numeric. Is it better to make my function tolerant of other types (option (A) below), or convert to a numeric before calling the function (option (B) below)?

# Option (A)
def numeric_operation(arg):
    i = int(arg)
    # do something numeric with i

# Option (B)
def numeric_operation(arg):
    # expect caller to call numeric_operation(int(arg))
    # do something numeric with arg
like image 855
fbarber Avatar asked Apr 22 '12 12:04

fbarber


4 Answers

If your function expects to operate on numeric data, then you are probably best off allowing Python to throw a TypeError if it doesn't receive one and something goes wrong. I would say, do the conversion outside and handle the exception.

def numeric_operation(arg):
   # Do numeric things

try: 
  numeric_operation("abc")
except TypeError:
  print("That was supposed to be numeric.")
like image 102
Michael Berkowski Avatar answered Nov 20 '22 21:11

Michael Berkowski


I would split these operations. Have one function to read numbers from a file, and let that function return real numbers or arrays. The functions doing the numeric operations shouldn't have to deal with conversions each time you call them, and you shouldn't have to implement it for each function. There might be exceptions, like a numeric function accepting numbers, lists of numbers and arrays alike.

When you put everything together you won't see any strings. Why should you? There are no real strings in the file you mentioned. These are numbers encoded as strings, so just read them accordingly and hide the conversion in the function that imports the data from your file.

like image 41
pwuertz Avatar answered Nov 20 '22 21:11

pwuertz


Don't do anything.

Python already has a great error-handling mechanism which will throw if you try to do something numeric with a string, and anything you do will just conceal the error you'd expect to get.

>>> def foo(i):
...     return i+1
...
>>> foo(3)
4
>>> foo("hello")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in foo
TypeError: Can't convert 'int' object to str implicitly
like image 1
Katriel Avatar answered Nov 20 '22 21:11

Katriel


It depends a lot on your use case. Your function name suggests that this is a strictly mathematical operation, in which case it probably doesn't make sense to accept strings. This is fundamentally a question of about separation of concerns; if your function does mathematics, it shouldn't also do conversion. So in this case I would advise converting first, and passing only numeric types to the function.

But if this were a function that did a lot of I/O, for example, it might be perfectly reasonable -- even preferable -- to call int on whatever is passed in. If your function is doing some weird set of transformations on your data to make it presentable to an end user, or to convert user input, those transformations should be encapsulated and grouped together as much as possible, so that the work they do doesn't "bleed" into other functions.

like image 1
senderle Avatar answered Nov 20 '22 20:11

senderle