1. 6

    Clearly a lot of work went into this post, but I found it less of a “state of type hints” and more of a “what and how”, in that I was expecting an overview of adoption, or progress in the type system. Having said that, it’s probably an excellent post for someone wondering “what are type hints in Python and how can I use them?”

    However, I’m pretty sure the final paragraph is incorrect?

    Remember that, similar to unit tests, while it does makes your code base contain an extra number of lines, at the end of the day all the code you add is code that is automatically checked and enforced to be correct. It acts as a safety net to ensure that when you change things around later on things keep working

    In a statically-typed language this would be true, but type hints in Python are just hints. There’s no guarantee from Python that either the hints or what’s passed is actually correct.

    1. 7

      While it’s true that Python’s type-hints aren’t automatically enforced, the same is true of tests: they don’t help either unless you run the test suite. At least for Python, comparing types and tests in this way seems reasonable.

      1. 3

        In a statically-typed language this would be true, but type hints in Python are just hints.

        I write a lot of Python and thus far I’ve eschewed type hints, for the simple reason that if I’m gonna go to all that effort I might as well just write Go.

        (I’m being slightly sarcastic, but there’s a kernel of truth in there.)

        1. 6

          I also write a lot of Python, and I type-hint 100% of it. The type hints typically take very little effort to write, and the benefits (IDE help + fewer bugs + easier refactoring) save me a lot of work, so on net it’s almost certainly effort-saving.

          (A bonus that’s possibly orthogonal to effort is that having to think about and write out the types I use encourages me to design cleaner APIs and makes a lot of bad code smells much more obvious.)

          1. 2

            Sure, and Python will still support that. Now imagine you’re an organization like Google and you have 100 million lines of Python in your company’s repo. It’s not going to be economically viable to rewrite it but you’d still like to incrementally add type checking to try to lower the rate bugs are found in Python code. That’s where Python’s type checking makes sense at the moment. I’ve still yet to see much adoption in the scientific Python space, for example, although I bet that’s mostly because NumPy doesn’t have type stubs yet.

            1. 3

              If wishes were fishes, one of the fish in my river would be to have something like [Elm’s record types], but for specifying Pandas data frame column names instead of record fields. Elm’s record types do not require you to specify all of the input’s fields; instead, you specify the ones the function expects, and the fields that are guaranteed to be in the output. The typechecker keeps track of which fields a record has as it passes through the functions.

              It would be perfect for the domain-specific cleaning and wrangling functions one writes on top of pandas. I realise this is not a trivial thing to wish for, though.

        1. 2

          as a non ruby programmer I wonder: What do you all need infinite ranges for that you need a special syntax for it. I think I never had a need for that. So I wonder, what are they used for?

          1. 2

            How about a game loop that keeps track of ticks?

            As a Ruby programmer I have definitely written infinite ranges a handful of times - it does come up, though it’s not common.

            1. 1

              there are examples in the blog post, but (also as a non ruby programmer) I don’t find any of them particularly compelling

              1. 1

                I am a Ruby programmer, and I’m also not sure what people need infinite ranges for so often.

                1. 1

                  I use them, but that’s probably me porting Hasellisms to Ruby more than a common Rubyism. Things like: (0..1.0/0).zip(array)

                  1. 1

                    Yeah it’s probably more Ruby-ist to use each_with_index for that, but it’s interesting that that works.

              1. 5

                I actually used infinite lists in my Python solutions this year. I can’t remember which one, but it was essentially “find the smallest number with some property”, which you can do like

                next(i for i in itertools.count(1) if has_property(i))
                

                I once gave a whole talk that was (in essence) about using Haskell-style infinite lists in Python:

                https://docs.google.com/presentation/d/1eI60SL3UxtWfr9ktrv48-pcIkk4S7JiDmeXGCyyGhCs/edit?usp=sharing

                Not to be all self-promoting, but it’s a fun talk.

                Anyway, good on you for doing it all in Haskell, I always aspire to try that, and then I always end up just doing it in Python.

                1. 2

                  I think the trick is do it with something you know well to get a feel for solving the problem, then try it in X.

                  Generators are great though - I used Racket’s generator support to implement the infinite spiral of day 3. I should look into doing it using the lazy language to just use regular list operations but lazily. I tried using streams but the performance was much worse - the garbage collector got overwhelmed which probably means I’m not using them correctly.

                1. 29

                  As someone who was in the position of the child not so long ago - please don’t do this*. Giving children without any explicit interest to learn about these things gifts trying to initiate some interest will fail for both 90% of the time. Sure, most people here would have loved (or were fortunate enough) to have been given technological gifts when they were children, but that’s easy to say now. If on the other hand you would have been giving something you had absolutely no interest in, or unfortunately no capacity to learn at that age, say a dictionary of ancient greek, an introduction to advanced arctic-geology, the collected works of Hegel or socks, and you know on some level that the person giving you the present is hoping for you to be as happy about it as they think they would have been - well that kind of “pressure” (for the lack of a better word) is not really a nice present, even if it was unintentionally. On the other hand, from the side of the person who gave the gift, unless you enjoy disappointment, you won’t feel much better either.

                  *: I’d like to clarify that I’m not trying to universally condem any gifts with the intention to boost a childs interest in some subject - just be sure that he or she has a potential to understand it, and know her or him good enough to be sure that they are the kind of person to be interested in it. Not every present is appropriate for every child. Thinking about it twice will prevent you from becoming the person who is trying to force his interest on children and your present to just disappear in a cupboard indefinitely.

                  1. 7

                    +1 to this, as the parent of a 6-year-old.

                    We have Robot Turtles (as mentioned in another thread) and we’ve played it quite a few times, and she simply doesn’t find it compelling. This is not intended as a knock on the game, I’m sure it’s great for a lot of kids, but different kids like different things. I bought the card game SET, and she gets it and will grudgingly play it with me but insists that it’s boring and that she’d rather do something else. I bought her “No Stress Chess” and she learned how the game works and how the pieces move but decided she would rather act out little dramas with the king and queen and such.

                    I’ll keep trying more things, but you can’t force kids into any of this stuff. (Or at least you shouldn’t, is my belief.)

                    I would love it if she wanted to learn coding, but this year for Christmas she really wants a Barbie that turns into a mermaid and also into a fairy, so that’s what she’s getting. Maybe next year.

                    1. 2

                      Edit: Previous comment didn’t really move the discussion forward, so here’s a new one.

                      Can you make your comment more constructive? Answers to some of the following would really help.

                      • Was there a certain approach, attitude, or expectation that put you off?
                      • How was “gift to try and initiate interest” conveyed? If it had been conveyed differently, like “toy that might resonate with deeper interest”, would you have had a better experience? What would each of these approaches look like to you?
                      • Is there a certain kind of gift/kit/etc that was too complicated/specialized/specifically about learning?
                      • Was there an interest of yours that had been mistaken for an interest in programming?
                      • Were there redeeming parts of your experience that could be illustrative for a better approach?
                      • Any specific input on what “he or she has a potential to understand it” means as it relates to your experience, or that would surprise a casual observer?

                      Surely there are ways to go about giving gifts that involve learning (not necessarily as a primary focus) that isn’t “pressure”.

                      I feel like you have an interesting perspective to share, but it’s all hidden behind a dismissive post. Even if your experience was an unmitigated disaster, there is something you could offer beyond “don’t even think about doing this”.

                      1. 4

                        Was there a certain approach, attitude, or expectation that put you off?

                        Not really “put me off “ - but I’d say that there was often an expectation that I already understood more than I did. In my case it was a electronics kit, but I didn’t know (and nobody told me (or at least I didn’t understand if if anyone did)) that electricity needs to flow in a circuit - and why should it? There’s only one wire from the plug hole to a lamp, why would this be any different?

                        How was “gift to try and initiate interest” conveyed?

                        To give an opposite example from my previous one - my grandfather, who was a professor of physics, once bought me some game (I can’t remember what it actually was, I was 5 y/o) that had to do with motors, moment and mechanics, etc. And he wanted to explain it all to me, but - not that I didn’t like it per se - but I just wasn’t interested in the physical stuff. There were little cut-out mammoths I found great delight with, and I remember my grandfather being disappointed to put it mildly that I didn’t want to play with the actual things…

                        If it had been conveyed differently, like “toy that might resonate with deeper interest”, would you have had a better experience?

                        … so it’s not really a problem of intention, or that’s at least not what I meant (I’m sorry if I was misunderstood). The issue just was that back then, I had e.g more interest in ancient animals than the laws of mechanics. So maybe it would have been different if I had an interest in physics, but for that I would have had to have had a basic understanding of the subject - without that - if all these things stay “mystical”, “magical” ideas beyond comprehension - I believe not much can be done to help the child develop an interest. So again, make sure the child is curious and capable (age and education wise) to engage with subject you want to introduce them to.

                        Is there a certain kind of gift/kit/etc that was too complicated/specialized/specifically about learning?

                        I’ve given examples already from my childhood, but for the most part I’d recommend not to give toolkits as first gifts. If one doesn’t have any idea what to do with it, or how to use it, it will either be forgotten or broken before one actually learns to use it properly.

                        Was there an interest of yours that had been mistaken for an interest in programming?

                        Well in my case it wasn’t programming, I had to teach myself all of that. Interestingly enough, I did always have a greater interest in things related to computers, but I guess my family were less interested in it, so they didn’t feel like supporting it. So the tip here would be to maybe transcend ones owns interest and actually try to support something the child actually likes.

                        Were there redeeming parts of your experience that could be illustrative for a better approach?

                        None of which I could think of spontaneously, I might edit the post later on if I come to think of something.

                        Any specific input on what “he or she has a potential to understand it” means as it relates to your experience, or that would surprise a casual observer?

                        “If the toy says ages 9-16, don’t give it to a 5 year old child” would be a good guideline. I’ve already implied it, but I’ll say it again, make sure the child’s first exposure isn’t this toy - 95% of the time this will go wrong, especially with younger children.

                        Surely there are ways to go about giving gifts that involve learning (not necessarily as a primary focus) that isn’t “pressure”.

                        Of course, the pressure I was talking about doesn’t (or at least in my case didn’t) come from the presents themselves, but the expectation from the people who gave them to me, to flourish or immediately develop a profound interest in the subject. I guess you could see this more as an attitude problem from the perspective of the gift-giver, but (depending of the child) he or she can feel that too. That’s the uncomfortable part, I really want children to be spared from.

                        I feel like you have an interesting perspective to share, but it’s all hidden behind a dismissive post. Even if your experience was an unmitigated disaster, there is something you could offer beyond “don’t even think about doing this”.

                        I apologize if my first commend was a bit too dismissive, I hoped my last paragraph would give the whole thing a positive turn, that’s why I added the footnote after the first sentence. But I hope I could clarify a few things now, and help you and anyone reading this with coming to an informed choice, when thinking about giving gifts with good intentions. Again, if it’s the right gift for the right person, it’s fantastic, but it’s not that easy to make sure that that is the case!

                        1. 4

                          if you are lucky enough to be able to work with the child and the gift, or you know their parents will be supportive, then you might create an interest, otherwise @zge comment is unforutnately the likely outcome - unless you know that they already have an interest in that area.

                          however, if the gift is fun and doable by the child then it can be a real success - although, the age on the tin is not helpful, my youngest is 7 years younger than her older siblings and she has aways played with age inappropriate toys :~)

                          my 2 pence worth from the perspective of a being a Dad :~)

                          1. 4

                            if you are lucky enough to be able to work with the child and the gift

                            I wish I could edit the OP as this is exactly the case, and there has been expressed interest.

                            1. 4

                              If it’s practical to do so, why not take the child somewhere where sciency toys are on display and see what he/she gravitates towards? I think if the learning is initiated by curiosity in the child then it’s more likely to have lasting effects.

                              I started taking guitar lessons when I was five years old because my granddad saw me staring at a guitar and he asked me if I wanted to learn (and I did). I don’t know how I would have reacted if I was just given an instrument as a gift without anyone asking beforehand what I thought about it.

                      1. 4

                        “In addition, Pytorch is quickly developing popularity amongst top AI researchers. Torch users, although nursing RSI injuries from writing Lua code to perform simple string operations, simply aren’t deserting in droves to Tensorflow – they are switching to Pytorch.”

                        Needs citation.

                        1. 4

                          I don’t know anything about the second, but I can confirm that PyTorch is quickly developing popularity within the AI research community, especially in NLP.

                          source: I work at an AI research institute

                          1. 1

                            I can also confirm. I’m a grad student studying ML and my group is switching to PyTorch. Plus, I’m recommending it to people I meet, something I didn’t do with Tensorflow.

                          2. 1

                            Here’s a citation. The CAT bytecode operation and table.concat are JIT compiled in LuaJIT 2.1, which is the version Torch uses by default.

                            So, either these people are using a 2+ year old version of LuaJIT, or their code is bad.

                          1. 7

                            yield from was actually the gateway drug that got me to upgrade to Python 3.

                            1. 1

                              FALT / INTP (but I am super Judgy about code style / practices in a way that I’m not about most things)

                              1. 3

                                I have no idea who this person is but that job sounds fiendishly interesting.

                                1. 8
                                  1. 1

                                    It sounds like she’s an SRE at Google.

                                    1. 7

                                      Not the one with all of the primary colors

                                      Is that a hint that it’s not google?

                                      1. 1

                                        Oh indeed! I thought that was referring to the recent rebranding of Google :)

                                      2. 3

                                        I think she used to be.