3 ms·
What is this vodoo syntax: x = list[1, 2, 3, 4] I assume yet another new bit of python syntax thats unguessable what it means...
by londons_explore 6y ago
What is this vodoo syntax:
x = list[1, 2, 3, 4]
I assume yet another new bit of python syntax thats unguessable what it means...
- travisjungroth 6y agoIt’s a type hint. But this is a tricky bug and it would normally be more obvious.
- deleted 6y ago[deleted]
- deleted 6y ago[deleted]
- duckerude 6y agoIt's nonsense that happens to not raise an exception. Typical use would look more like this: x: list[int] = [] i.e. x is a list of integers that starts out empty. It doesn't automatically do anything at runtime, it's basically a glorified comment for tools and libraries to use. So that does make it harder to figure out what it is.
- filmor 6y agoThat is completely normal syntax that "always" worked with the right thing named `list`.
- dmurray 6y agoIt worked if "list" was specifically a dictionary one of whose keys was the tuple (1, 2, 3, 4), and only then because Python allows you to omit the parentheses around the tuple in most situations. Or it could work with a custom type with overridden operators through __getitem__. It's a stretch to call it "completely normal" though I believe you're correct in a strict sense: you could always construct the rest of your program such that it would contain that line and still compile. It would be extremely bad style.
- duckerude 6y agoNumpy supports that style of indexing for multidimensional arrays, so it's not that outlandish. Not something you'd use with the standard library though. I guess I wouldn't call it "completely normal" either.
- meowface 6y agoIt'd only be bad style due to the confounding and misleading variable name, not necessarily due to the operation or potential operator overloading.
- Too 6y agoSome new stuff in 3.9: https://www.python.org/dev/peps/pep-0585/ https://www.python.org/dev/peps/pep-0585/ Basically they alias all of the old builtins like list to typing.List etc. (Or maybe one should say the goal is to un-alias the alternative type system in typing-module and only use the original builtins). It can be useful when you for example want to create a type alias like UserList = typing.List[User], which was possible even before, the change is that it now affects the old builtin also, so things that were previously syntax errors are now valid type aliases. Usually such alias assignements are more useful once you have more complex generics with nested dicts and stuff.