Skip to main content
added 276 characters in body
Source Link

You are certainly allowed to do input checking. In some languages, you might check input arguments to make sure they are the appropriate type (i.e., they are of the right class). However, in Python, it is often preferred to check whether the input exposes the necessary capabilities. This is sometimes called Duck Typing, after the maxim that if something walks like a duck and quacks like a duck, it's probably a duck--or at least something that a duck processing function should be able to handle.

To give you a more concrete example, suppose you are writing a function that calculates the mean of a list of numbers. Instead of checking whether the parameter is, in fact, a list, check whether it is iterable (or even better, just try to iterate over it and throw an exception if not). This lets your function work for lists, tuples, sets, files, values from databases, and even as-of-yet-uninvented objects that store collections of numbers. On the other hand, you are certainly allowed, and even encouraged, to do domain-specific checking. If your code receives mathematically nonsensical values (e.g., negative probabilities), it probably should throw an exception or somehow signal an error.

Overall, The Python philosophy seems to be "check as much as you need to, but no more. If the caller does something strange, it's at least partly their problem (like regressing on a string). That said, these are guidelines, not hard and fast rules, so use common sense.

You are certainly allowed to do input checking. In some languages, you might check input arguments to make sure they are the appropriate type (i.e., they are of the right class). However, in Python, it is often preferred to check whether the input exposes the necessary capabilities. This is sometimes called Duck Typing, after the maxim that if something walks like a duck and quacks like a duck, it's probably a duck--or at least something that a duck processing function should be able to handle.

To give you a more concrete example, suppose you are writing a function that calculates the mean of a list of numbers. Instead of checking whether the parameter is, in fact, a list, check whether it is iterable (or even better, just try to iterate over it and throw an exception if not). This lets your function work for lists, tuples, sets, files, and even as-of-yet-uninvented objects that store collections of numbers.

Overall, The Python philosophy seems to be "check as much as you need to, but no more. If the caller does something strange, it's at least partly their problem (like regressing on a string). That said, these are guidelines, not hard and fast rules, so use common sense.

You are certainly allowed to do input checking. In some languages, you might check input arguments to make sure they are the appropriate type (i.e., they are of the right class). However, in Python, it is often preferred to check whether the input exposes the necessary capabilities. This is sometimes called Duck Typing, after the maxim that if something walks like a duck and quacks like a duck, it's probably a duck--or at least something that a duck processing function should be able to handle.

To give you a more concrete example, suppose you are writing a function that calculates the mean of a list of numbers. Instead of checking whether the parameter is, in fact, a list, check whether it is iterable (or even better, just try to iterate over it and throw an exception if not). This lets your function work for lists, tuples, sets, files, values from databases, and even as-of-yet-uninvented objects that store collections of numbers. On the other hand, you are certainly allowed, and even encouraged, to do domain-specific checking. If your code receives mathematically nonsensical values (e.g., negative probabilities), it probably should throw an exception or somehow signal an error.

Overall, The Python philosophy seems to be "check as much as you need to, but no more. If the caller does something strange, it's at least partly their problem (like regressing on a string). That said, these are guidelines, not hard and fast rules, so use common sense.

Source Link

You are certainly allowed to do input checking. In some languages, you might check input arguments to make sure they are the appropriate type (i.e., they are of the right class). However, in Python, it is often preferred to check whether the input exposes the necessary capabilities. This is sometimes called Duck Typing, after the maxim that if something walks like a duck and quacks like a duck, it's probably a duck--or at least something that a duck processing function should be able to handle.

To give you a more concrete example, suppose you are writing a function that calculates the mean of a list of numbers. Instead of checking whether the parameter is, in fact, a list, check whether it is iterable (or even better, just try to iterate over it and throw an exception if not). This lets your function work for lists, tuples, sets, files, and even as-of-yet-uninvented objects that store collections of numbers.

Overall, The Python philosophy seems to be "check as much as you need to, but no more. If the caller does something strange, it's at least partly their problem (like regressing on a string). That said, these are guidelines, not hard and fast rules, so use common sense.