3 Retag dynamic and typing to dynamic-typing.
| link
2 added 53 characters in body
source | link

In statically-typed languages such as Java, code such as the following is common (not much of a realistic example, I know):

public String flip(String text) {
    String result = "";
    for (int i = text.length() - 1; i >= 0; i++) {
        result += text.charAt(i);
    } 
    return result;
}

public int flip(int value) {
    return (-1) * value;
}

However in dynamic languages, there's no way to dispatch a specific method based on a parameter's type. So for example in Python 3, we'd have two options on how to do this. First is to check the type inside the function.

def flip(value):
    if isinstance(value, str):
        return value[::-1]
    elif isinstance(value, int):
        return (-1) * value
    else:
        raise TypeError('Expected str or int.')

Second option is to have multiple functions:

def flip_int(value):
    return value * (-1)

def flip_str(text):
    return text[::-1]

(In dynamic languages such as Python we should try to employ duck-typing, but I'm talking about cases where that's not possible, such as the example above).

Which option should I prefer, and why? Also, is there a 'best-practice' for this in Python?

I'm asking especially about Python, but this is relevant I believe to any dynamic language.

In statically-typed languages such as Java, code such as the following is common (not much of a realistic example, I know):

public String flip(String text) {
    String result = "";
    for (int i = text.length() - 1; i >= 0; i++) {
        result += text.charAt(i);
    } 
    return result;
}

public int flip(int value) {
    return (-1) * value;
}

However in dynamic languages, there's no way to dispatch a specific method based on a parameter's type. So for example in Python 3, we'd have two options on how to do this. First is to check the type inside the function.

def flip(value):
    if isinstance(value, str):
        return value[::-1]
    elif isinstance(value, int):
        return (-1) * value
    else:
        raise TypeError('Expected str or int.')

Second option is to have multiple functions:

def flip_int(value):
    return value * (-1)

def flip_str(text):
    return text[::-1]

(In dynamic languages such as Python we should try to employ duck-typing, but I'm talking about cases where that's not possible, such as the example above).

Which option should I prefer, and why?

I'm asking especially about Python, but this is relevant I believe to any dynamic language.

In statically-typed languages such as Java, code such as the following is common (not much of a realistic example, I know):

public String flip(String text) {
    String result = "";
    for (int i = text.length() - 1; i >= 0; i++) {
        result += text.charAt(i);
    } 
    return result;
}

public int flip(int value) {
    return (-1) * value;
}

However in dynamic languages, there's no way to dispatch a specific method based on a parameter's type. So for example in Python 3, we'd have two options on how to do this. First is to check the type inside the function.

def flip(value):
    if isinstance(value, str):
        return value[::-1]
    elif isinstance(value, int):
        return (-1) * value
    else:
        raise TypeError('Expected str or int.')

Second option is to have multiple functions:

def flip_int(value):
    return value * (-1)

def flip_str(text):
    return text[::-1]

(In dynamic languages such as Python we should try to employ duck-typing, but I'm talking about cases where that's not possible, such as the example above).

Which option should I prefer, and why? Also, is there a 'best-practice' for this in Python?

I'm asking especially about Python, but this is relevant I believe to any dynamic language.

1
source | link

Type checking as opposed to multiple functions?

In statically-typed languages such as Java, code such as the following is common (not much of a realistic example, I know):

public String flip(String text) {
    String result = "";
    for (int i = text.length() - 1; i >= 0; i++) {
        result += text.charAt(i);
    } 
    return result;
}

public int flip(int value) {
    return (-1) * value;
}

However in dynamic languages, there's no way to dispatch a specific method based on a parameter's type. So for example in Python 3, we'd have two options on how to do this. First is to check the type inside the function.

def flip(value):
    if isinstance(value, str):
        return value[::-1]
    elif isinstance(value, int):
        return (-1) * value
    else:
        raise TypeError('Expected str or int.')

Second option is to have multiple functions:

def flip_int(value):
    return value * (-1)

def flip_str(text):
    return text[::-1]

(In dynamic languages such as Python we should try to employ duck-typing, but I'm talking about cases where that's not possible, such as the example above).

Which option should I prefer, and why?

I'm asking especially about Python, but this is relevant I believe to any dynamic language.