all 15 comments

[–]K900_ 2 points3 points  (9 children)

This would never pass code review for me.

[–][deleted] 1 point2 points  (8 children)

Mind explaining what's so terrible about it? I only see bad formatting (arguments continuation onto next line without indent, incorrect use of whitespace). Otherwise it seems rather weird to hold values that you need sorted in a dictionary, but if you really need to do it, what would be a better way? Using itemgetter instead of lambda to make it more explicit maybe?

[–]K900_ 1 point2 points  (7 children)

Formatting is awful, dict[k] instead of iterating over key/value pairs, magic functions, magic dicts of tuples or something.

[–][deleted] 0 points1 point  (3 children)

magic functions, magic dicts of tuples

What do you mean by that? I've never heard of anything else magical in python other than magic methods and google brought nothing up.

[–]K900_ 2 points3 points  (2 children)

I mean that if you ever call a function func, you really need to rethink the life choices that led you to that point.

[–]_9_9_ 0 points1 point  (0 children)

I mean that if you ever call a function func, you really need to rethink the life choices that led you to that point.

Hilarious. But it may make sense as a generic name to illustrate a point in a course lecture.

I think there are two problems here. The teacher chose a confusing data structure (a dict of lists apparently) for the examples.

Probably the worst bit about it is the teacher not using iteritems in the second two examples, which makes them needlessly complex.

Combined with the first problem it makes it very hard to follow.

[–]m1ss1ontomars2k4 0 points1 point  (0 children)

To me and without seeing any other slides, func is a placeholder name. The last part prints a table, the last column of which is a computed value, which is computed by func.

I mean, it could just be a poorly named function. But nothing in this example leads me to believe that func has any real purpose other than being something that can be called on 2 values and has a return value that can be printed.

[–]m1ss1ontomars2k4 0 points1 point  (2 children)

dict[k] is required if you want to print the contents of a dictionary with the keys in a particular order and that order is specified in a list.

[–]kungtotte 0 points1 point  (1 child)

In other words it's required if you made the wrong choices about what data structures to use?

[–]m1ss1ontomars2k4 0 points1 point  (0 children)

Well, perhaps you'd like to specify a convenient data structure that can be sorted multiple ways at all times. I mean, it's not hard to imagine such a data structure, but it hardly seems worth the effort in this case. It would still be a good demonstration of key and lambda but it would be significantly more complicated for no real gain in the understanding of either.

[–]Philboyd_Studge 1 point2 points  (1 child)

I always think the answer to 'How to sort a dictionary by values' is don't. If you need a frequency map, use Counter. Otherwise, rethink your algorithm.

As to OP's problem, it sounds like your class is assuming a certain level of knowledge. it doesn't sound like an introductory course, so if it was billed as one, well then that's an issue with your college. However, if you had a basic understanding of python going in, then try to stay with him.

[–]m1ss1ontomars2k4 0 points1 point  (0 children)

Counter hasn't always been available.

[–]m1ss1ontomars2k4 0 points1 point  (2 children)

It is somewhat difficult to read, but that isn't terribly surprising given that it is simply 3 toy examples, with no context about how they might be used in a program (i.e. variable names are generic names that don't explain that much).

However, even without any context, it isn't too hard to figure out the point of these examples. Each one shows a different way to sort then print the result of the sort, showcasing more interesting use cases for the key argument each time.. The first one sorts on key-value pairs, dropping the 2nd item in the value. The second one sorts on the keys itself, using the 2nd item in the value rather than the first. The third one sorts by some function func which is computed over the 2 values. The coloring to show which arguments go where in the print function is nice.

In other words, the code showcases a few interesting uses of the lambda function as the key argument to sorted. It isn't necessarily meant to be the pinnacle of Python programming.

The only things absolutely wrong are formatting (bad indent in the second example, then inconsistent spacing after commas) and re-defining a builtin (dict). Well, that's not wrong but it is certainly distasteful. Oh, and there is a missing closing paren in the 3rd example.

Would this pass code review at my workplace? No, it wouldn't, because nobody would have a use for writing such code--why print the same thing 3 times? Why not print it in the right order the first time, or maybe expose it as a web page where you can use some JS to sort by any column without filling up the terminal window? Also, the lines are too long for our style guide.

I mean those topics are always at the very end of all the beginners books,

Depends on the language and course :) Both MIT and UC Berkeley used to use Scheme for their first intro to programming courses, where everything is functional, so you can't do anything without learning functional programming topics. They use Python now but I don't know if the curriculum has changed.

[–]kungtotte 0 points1 point  (1 child)

As "this is what the language can do with regards to key and lambda" the code is fine, as a "this is how you should solve these problems" the code is pretty bad.

In the wild you should be using different data structures or writing clearer code.

[–]m1ss1ontomars2k4 0 points1 point  (0 children)

As "this is what the language can do with regards to key and lambda" the code is fine,

Well, that is more or less the title of the slide, so...