3 ms·
> introductory > object-oriented programming Unless I was mandated to make my syllabus match the language reference's table of contents, I would defer most of
by csense 13y ago
> introductory
> object-oriented programming
Unless I was mandated to make my syllabus match the language reference's table of contents, I would defer most of object-oriented programming to a second semester. How to use particular objects from the standard library would likely be covered on an as-needed basis in an introductory class -- for example, "you use the syntax d.keys() to get a list of a dictionary's keys" without attempting to explain that "keys" is a function attribute of a class of which d is a member, and thus by MRO rules d.keys triggers a lookup of the "keys" member from the built-in dict class, and d will be passed as the first parameter when the function call operator denoted by the parentheses is executed ...You may be tempted to include such detail for various reasons, but trust me, your students will thank you if you save that content for a more advanced course.
> There is no way to teach integer logic really (Python has auto-growing arbitrary precision integers)
You can still teach binary representation and use bit-wise operators.
Also, I once wrote a storage-efficient bit vector class with the built-in bytearray module, which is a good exercise to tie together object-oriented programming and binary representation. However, a student at the level of doing that exercise is no longer beginner, but firmly intermediate or advanced.
> floats can lead to strange errors
These are a natural consequence of the floating-point representation and this problem exists in each of the article's top three language choices.
- Derbasti 13y agoRe floats: True, this problem is universal. I still wish there was a way to circumvent it, much like the arbitrary precision integers circumvent integer problems. Rationals and fractional integers come to mind, though I don't know their performance characteristics.
- sdevlin 13y agoPython has a decimal package in stdlib. It provides an arbitrary-precision floating point type. The performance will obviously be worse than with floats, but I doubt it matters in an intro programming class. The only real drawback is that there are no decimal literals. Actually, several languages I know of have decimal types in stdlib. Two that do not are C and JavaScript.
- lhl 13y agoWhile obviously it's a fundamental thing to teach/cover the various data types (or at least how '1', 1, and 1.0 are different since they'll be encountering this over and over if they continue programming), on a practical level, frequent casting can be sidestepped by using string formatting when printing numeric output. In all my production code (my string printing is not performance critical) I pretty much always use string substitution when printing as a matter of style/convenience. It's much easier to add/replace substitutions, and I often need formatting anyway. If students get used to printing w/ string substitution, they can then simply use %d or %f to control how they want their numbers to display, which is probably the right way of thinking about rendering text output.
- csense 13y agoPython has the "fractions" module which provides a more powerful abstraction: It's not restricted to denominators that are powers of 10. > I don't know their performance characteristics It's easy to make some good guesses. Since (I'm guessing) it's implemented with pairs of integers, adding probably uses a / b + c / d = (ad + bc) / (cd) which is 3 multiplications and an addition, and multiplication probably uses (a / b) (c / d) = (ac) / (bd) so only two multiplications. So it's a small constant factor times the cost of integer multiplication, plus a bit of overhead for the wrapper class. So my back-of-the-envelope guess is that the fractions module is less than 10x slower than the same operation with integers, or fast enough for just about anything you do in a beginner level class. These numbers will change significantly if fractions are automatically reduced by gcd of numerator and denominator; I'm not sure about this point.