4 ms·
The problem is that: - object castings are not nice (for code brevity/clarity - things, which Python aims to bee good at), - if objects are similar, it's pain
by stared 11y ago
The problem is that:
- object castings are not nice (for code brevity/clarity - things, which Python aims to bee good at),
- if objects are similar, it's pain to explain the difference (it's not a thought - it's a thing I had to deal with many times).
In particular, if one is advanced programmer in general, or at least has a firm grasp of Python basics, iterators are nice. But for newbies, they are harder. (And it's even harder to hand-wave that they are "somethings, which can be casted into lists".)
As a side note I dislike possibility of using tuples as iterables. It breaks "There should be one-- and preferably only one --obvious way to do it." i.e. using tuples in place where one (conceptually) should use a list.
And again, explaining difference to newbies is painful ("tuple is a handicapped list").
- agentultra 11y agoI think the real problem here is pedagogy. I don't begin teaching someone programming by explaining objects and types. I generally start by introducing three fundamental concepts: variables, conditionals, and loops. And I keep it simple to begin with: a = 1 print(a) # 1 a = 2 print(a) # can you guess what it will print? Then I add conditionals: if a == 2: print("it is two!") else: print("it is not two...") And I only really cover 'for' at first: groceries = ["ham", "cheese", "eggs"] for item in groceries: print(item) Along the way I explain some of the primitive data types: string, integer, float; and containers such as list and dictionary. And that is usually enough to get started with simple tasks. I tend to elide what functions are and just call them "commands" until later on so that I can demonstrate why looping is so cool: import turtle turtle.setup() for amount in range(100): turtle.forward(amount) turtle.left(75) And that usually drives home the point: grouping commands together to repeat them as many times as we wish using loops; variables hold data; and conditionals let us do different things depending on whether something is True or False. I haven't had much trouble with this approach for years. I don't even get to explain iterables to newbies most of the time! Once in a while someone tries something like: a = "foo" a + 1 And they get TypeError or they pass in an object of the wrong type to a function and get ValueError. Early on this usually isn't a problem because some things just don't make sense like adding numbers and strings. However it can get confusing when learning how to look up functions and use them because we can only informally document what kinds of things a given function will take in its signature... it's an advantage and disadvantage of the duck-typing philosophy. It's a wart but one that I haven't really encountered with anyone I've taught until they're pretty far along and able to help themselves.