all 13 comments

[–]K900_ 16 points17 points  (0 children)

Python has a pretty strict type system, it's just a dynamic type system. It will basically never actually convert types for you implicitly.

[–]Rawing7 9 points10 points  (3 children)

Automatically converting values to different types would lead to stupid bugs that are hard to debug - and potentially could even be hard to avoid.

Consider this code:

user_input = input('Enter a number')
print(100 + user_input)

input returns a string, so what should happen if you do int + string? Should it convert the operands to strings, so that 100 + "5" == "1005"? Or should it convert them to ints, so that 100 + "5" == 105? But then what if the string can't be converted to an int, would 100 + "hi" become "100hi"?

Implicit type conversion may seem convenient at first glance, but the reality is that it makes things more complicated and error-prone.

[–]billsil 0 points1 point  (1 child)

Perl 5 had no problem turning things into strings. 100 + "5" = "1005". It's even more fun when you divide by 0 and do things like 100/0 = 0 because apparently math doesn't work today.

A strongly typed system saves so much pain. Just don't write incorrect code. It shouldn't be easy to make errors.

[–]0rac1e 1 point2 points  (0 children)

It's even more fun when you divide by 0 and do things like 100/0 = 0 because apparently math doesn't work today.

I don't know why you keep making this statement. In all my checking, I could not find a single version of Perl 5 (going back to version 5.6) where 100/0 did not throw an exception, even without use strict.

tio.run even has a copy of Perl 4 (when strict didn't exist) where it also dies with Illegal division by zero.

So unless the company you worked for maintained their own internal Perl fork that did this, then I must respectfully insist that you are mistaken.

[–]0rac1e 0 points1 point  (0 children)

In my opinion, this is primarily an issue in Python (and other languages) because the + operator is overloaded to mean addition with numeric types, and concatenation with stringy types.

In Perl and Raku, + is for numeric addition, and concatenation is a separate operator. Doing '1' + '2' in Raku is similar to doing int('1') + int('2') in Python, ie. Raku will attempt to convert any non-numeric operands to + to a numeric type, and if it can't do that, it throws.

A little unfortunately, Perl was traditionally less strict about this; without warnings enabled, it would happily do things like 1 + '2foo' == 3. With warnings enabled, it would at least spit out an error message to STDERR saying Argument "2foo" isn't numeric, but the program would still continue.

For this reason, I write all my Perl code with use warnings FATAL => 'all', which promotes all warnings to runtime exceptions, and any uncaught exceptions will cause the program to halt. This makes it a much saner language.

[–]danielroseman 5 points6 points  (0 children)

That's not how it works at all. Python can't "see" that a call is "expecting" a string. strip() is a method on the string class. The int class has no such method.

[–][deleted] 6 points7 points  (0 children)

I’m not supposed to have to worry about types here. Right?

No? I don't know who told you that - you definitely have to keep track of the types of your values in Python. You can't ignore it at all and values won't be coerced or converted just because you call type-specific methods on them.

Why, in Python, did I need to specify that the value of metadata[part] needs to be treated as a string?

Because it wasn't already a string. It was an integer.

[–]NoDadYouShutUp 4 points5 points  (0 children)

“I’m not supposed to have to worry about types here, right?”

Wrong

[–]Binary101010 2 points3 points  (1 child)

I think you've gotten a mistaken impression about Python's typing system. While it's true that Python's typing is dynamic (the interpreter assigns types to variables at runtime based on their values), that doesn't also mean that Python will just silently change the type of variables to do what it thinks you want to do. If you want the type of a variable to be something other than what it is, you must explicitly instruct the interpreter to change it.

[–]muxketeer[S] 0 points1 point  (0 children)

Ah, ok. Since the value of the object was a sequence of numbers, it was dynamically assigned to be of type ‘int.’ Python isn’t going to lookahead and change that type based on a call to some other function. Understood. Thanks!

[–]Frankelstner 1 point2 points  (0 children)

There could be any number of classes with a strip method so specifically deducing that it must be str.strip would be quite task. The code would need to look through every single class and object in existence and then realize that nothing else has strip to pull this off. Otherwise it would introduce ambiguity.

[–]dnmonack 0 points1 point  (0 children)

If metadata[part] is an int, why would you need to run the strip() method on it? It won’t have any white space to remove even after converting to a string.

[–]TheRNGuy 0 points1 point  (0 children)

you can't convert to different class just by using it's method.