Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> It is not possible for a from zero beginner to read ordinary Python code with decorators, generators or what not and understand it.

Having taught beginners both python and other languages, I’d say it’s significantly easier to teach decorators than it is to teach higher order functions normally. Because in python the students have already seen, used and understood what the cache decorator does before we even touch the concept of a function returning another function, which they always need some time to grok, both in python and other languages.

As for generators I’ve never seen anyone struggle with them. What do you find difficult to understand about generators?



Generators have a few footguns even for seasoned developers, calling len(mygen) or mygen[4] works, but after that it's empty when trying to iterate it. Particularly problematic is when someone adds a log of the generator content, exhausting it before before it is used. All this leads to people shotgunning mygen=list(mygen) on everything, without understanding what really happens.

How one could solve this i don't really know, generators are such useful concept that you can't really remove them either, and you still want them to be compatible everywhere other lists and iterables are. Even with type hints, it's not exactly obvious when you type the code whether a certain variable is a generator or a concrete list, both of them mostly adhere to the same protocol.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: