r/programming • • Jun 27 '18

Python 3.7.0 released

https://www.python.org/downloads/release/python-370/
2.0k Upvotes

383 comments sorted by

View all comments

Show parent comments

11

u/caramba2654 Jun 28 '18

Not that anyone sane would do that. Just because it can be done it doesn't mean it should be done.

And if you try searching Github for any code that does what you described, I don't think you'll find any instances of it.

15

u/wavy_lines Jun 28 '18

Why are you assuming it to be insane? How do you think ORM libraries work?

I'm not saying anything here about weather it's good or bad. I'm just pointing out that Python is not strongly typed because the type almost means nothing and you can do whatever the hell you want to the object.

You don't have to call delattr or setattr. Just simply take any instance and assign fields to it:

some_object.some_field_1 = <Some-Value>

It doesn't even have to be malicious. It could be an honest mistake. You thought some_object was an instance of some class that does have some_field_1 and nothing about the language runtime would even warn you that you're doing anything wrong.

27

u/caramba2654 Jun 28 '18

But strong typing doesn't reflect what attributes an object has. Strong typing means that there's no automatic coercion of a value of type A to a value of type B. And Python works exactly like that. So by definition Python is strongly typed.

What you should be claiming instead is that Python is dynamically typed, which is the property that allows you to add and remove attributes to an object.

2

u/wavy_lines Jun 28 '18

That's not a very useful definition because the scenario I presented above matters and it's a significant problem in Python. Excluding it from the definition of strong typing serves no objectively useful purpose.

18

u/Tynach Jun 28 '18

Strong and static are not the same thing. What you're talking about has to do with static typing, which is different from strong typing. Python is dynamically and strongly typed. Not 'or', but 'and'. C is statically and weakly typed (types get coerced a lot in C; like how char is often used as either a number or a letter).

5

u/wavy_lines Jun 28 '18

That's not what static typing means.

Static means you can just analyze the code without executing it to find out typing errors.

Dynamic means you can't do complete type checking by just looking at the code; you have to run it.

13

u/Tynach Jun 28 '18

Static means that what type a variable is will be determined at compile time, and cannot change at runtime. Strong means that there are no implicit/automatic conversions from one type to another, so a programmer must explicitly perform type conversions themselves in the code.

I found a chart that roughly shows which languages are which, on both axes.

→ More replies (0)

4

u/baerion Jun 28 '18

It's insane that you're getting downvotes for this in proggit. Your definition of static and dynamic typing is actually the right one.

2

u/caramba2654 Jun 28 '18

I know, but it's not we who define what strong typing is. What your concerns refer to is dynamic typing. If you were to complain about strong typing, you'd have to complain about C, C++ and Javascript, but definitely not Python.

1

u/wavy_lines Jun 28 '18

"but it's not we who define what strong typing is"

We who? Programmers? Yes it's definitely us programmers who get to define these terms.

If enough people see that the existing mainstream definition of "strongly typed" is bogus, then it will cease to be an accepted meaning of the term.

I actually have no complaints about C in this regard because if you try my_struct.some_field and some_field does not exist on the type of my_struct then it will give you a compile time error. This is much stronger than anything python can ever dream of, because python doesn't even define what set of fields are available on any given object.

Now, automatic casting between chars/ints/etc is certainly a bad idea, but python's non-sense with not defining an object's fields is a much much worse idea.

EDIT: actually python does have a way to restrict what set of field names are available on an object via the __slots__ mechanism, but hardly anyone uses it, let alone know about it.

3

u/caramba2654 Jun 28 '18

We who? Programmers? Yes it's definitely us programmers who get to define these terms. If enough people see that the existing mainstream definition of "strongly typed" is bogus, then it will cease to be an accepted meaning of the term.

Yes, but right now as of today it means what I said before, and using strong typing to refer to anything else just because you want it to doesn't really help you being understood by others.

I actually have no complaints about C in this regard because if you try my_struct.some_field and some_field does not exist on the type of my_struct then it will give you a compile time error. This is much stronger than anything python can ever dream of, because python doesn't even define what set of fields are available on any given object.

Again, this refers to dynamic typing, not weak/strong typing. C is statically typed and weakly typed. Javascript is dynamically typed and weakly typed. Rust is statically typed and strongly typed. And Python is dynamically typed and strongly typed.

I'm not arguing whether being able to delete/add fields to an object at runtime is bad or not. All I'm saying is: if you dislike languages that allow that, then you should seek statically typed languages.

→ More replies (0)