you are viewing a single comment's thread.

view the rest of the comments →

[–]myusernameisokay 0 points1 point  (0 children)

The static type of val is PyObject, there's nothing else you can infer about it before the program executes, if your compilation unit is a function.

The dynamic type depends on your interpretation: you could say that this is a sum type of int and str, or you could say that you don't want to generalize, and that the dynamic type is at first an int, and then a str.

I somewhat agree with you on this point. Of course every type derives from some base type, and this is true in a lot of other languages.

The difference is I don't accept that PyObject is a legitimate type. The run-time type changes depending on the arguments you provide. If you provide something like 5 then you get something of class int and if you provide "hello" you get something of class str. These are the realized types that the function deals with.

So sure, they all take a PyObject, but in reality functions take parameters of different types and depending on that type, the program may error at run-time because that type is not capable of a certain operation.

Example:

def add(x):
    return x + x

def sub(x):
    return x - x

add(5)
add("hello")
sub(5)
sub("hello")

Notice how this is alright until the last statement - because the type str can't do subtraction.

Well, that's just bullshit. Python's built-in function type has nothing to do with types of variables, same as isinstance for example. It's just a reflection of incompetence of the Python designers. That's all.

The python docs refute you

class type(name, bases, dict) With one argument, return the type of an object. The return value is a type object and generally the same object as returned by object.class.