6 ms·
Strongly typed: >>> "foo" + 3.141 TypeError: can only concatenate str (not "float") to str >>> object() + 3.141 TypeError: unsupported operand
by blattimwind 7y ago
Strongly typed:
>>> "foo" + 3.141
TypeError: can only concatenate str (not "float") to str
>>> object() + 3.141
TypeError: unsupported operand type(s) for +: 'object' and 'float'
Weakly typed:
> "foo" + 3.141
"foo3.141"
> Object() + 3.141
"[object Object]3.141"
> [] + {}
"[object Object]"
> {} + []
0
- msla 7y ago> "foo" + 3.141 "foo3.141" How is this weakly typed when it uses type information to work? No, this is strongly typed.
- jsjddbbwj 7y agoa = 3 a = 'abc' There goes your strength, Samson. Just because there's something worse, that doesn't make python strongly typed
- blattimwind 7y agoYou are conflating weak/strong-typing with static/dynamic-typing. These are largely orthogonal.
- Macha 7y agofn main() { let x = 1; let x = "foo"; } Does this make rust not strongly typed? Here's a Rust program with no types in the source code. It seems the issue you're objecting to is that python doesn't differentiate variable declaration from assignment (The fact we need let twice in this code is a result of Rust doing this). Which is a fair thing to complain about (and why Python had the "nonlocal" and "global" keywords), but is not the same as being strongly or weakly typed.
- steveklabnik 7y agoThat's not an identical translation, the identical Rust would be fn main() { let mut x = 1; x = "foo"; } which indeed fails to compile with a type error. (That being said there is a conflation of static/dynamic and weak/strong going on in this thread, as there always is in these kinds of discussions.)
- joshuamorton 7y agoI'd disagree that this is a better translation. In python-land, `x` is just a name binding. The closest thing might be that `x` is something akin to a Box<T>, but I don't know that that's cleanly expressible in rust. Like in (modern) python you can totally do def foo(): x: Union[str, int] = 1 x = "foo" which would be akin to in rust ?? (sorry my rust foo isn't great). Specifically the semantics don't work here because if you do ~this: def foo(): x = 1 async takes_int(x) x = "foo" this will always work find in python (even in a hypothetical GIL-free python, even if you make the assignment actually async), whereas that wouldn't work in rust if you pass a mutable ref to takes_int (at least if memory serves). Or I guess another way of putting this is that names in python can't be mutable.
- hope-striker 7y agoYou seem to be confusing mutable variables with mutable references. A name, in Python, is a mutable cell that holds a reference. Python names definitely correspond to mutable, not immutable variables in Rust.
- joshuamorton 7y agoWell no, for the reason I describe above: if you have the pattern mut a = 4 f(a) print(a) In rust and python, you'll always get 4 in python, but the value in rust depends on `f`. This means that the passed variable is immutable but shadowable, as in rust. (An object in python is much more like an Box/Cell, so the contained object can be mutated, but the reference to the box itself is immutable).
- hope-striker 7y agoThe value in Rust does not depend on f. I will be precise. There is no definition of f such that this function will print anything other than "4". fn main() { let mut x = 4; f(x); println!("{}", x); } Again, you seem to be confusing mutable references and mutable variables. If I had written f(&mut x) rather than f(x), you would be right.
- filleduchaos 7y agoYes, obviously, Python isn't statically typed. I fail to see how this demonstrates a lack of strong typing though.
- bfrog 7y agoname shadowing isn't the same as strong typing, you can do the same thing in rust today https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=fdf52c2ca5cdd74e6612e6497829fa63 https://play.rust-lang.org/?version=stable&mode=debug&editio...
- hope-striker 7y agoReassignment isn't shadowing. See https://news.ycombinator.com/item?id=22444824 https://news.ycombinator.com/item?id=22444824 .
- joshuamorton 7y agoSteve's wrong. Python names can't be made mutable.