[Tutor] all right students, what do we learn
Clayton Kirkwood
crk at godblessthe.us
Sun Nov 2 21:18:56 CET 2014
>-----Original Message-----
>From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On
>Behalf Of Dave Angel
>Sent: Sunday, November 02, 2014 1:18 AM
>To: tutor at python.org
>Subject: Re: [Tutor] all right students, what do we learn
>
>On 11/02/2014 01:43 AM, Clayton Kirkwood wrote:
>> To prove that a little knowledge is a little stupid or something to
>> that
>> effect:
>>
>> # for key in key_list:
>>
>> # print(key)
>>
>> # if key not in key_list0:
>>
>> # print("Error:", key, "not available, start again")
>>
>> # get_new_list = True
>>
>> # break
>>
>> # else: get_new_list = False
>>
>>
>>
>> Assume key_list0 = [('a', 'Ask'), ('y', 'Dividend Yield')]
>>
>>
>>
>> The above worked when key_list was 'a y' or 'hhh'
>>
>
>For what definition of "worked"?
>
>You have no comments as to what the loop is supposed to produce, nor
>what its inputs are supposed to look like. Neither of the values for
>key_list are lists, so what's up with that? Further, it appears you
>want to compare one-letter strings to one element of a tuple.
>
>>
>>
>> Well, if a little cool was good why not use all of my new found
>> knowledge of comprehensions.
>
>Why? There are ways to simplify it, but list comprehension isn't one of
>them. I'd be looking into sets.
>
>Something like (untested):
> get_new_list = not set(key_list).issubset(set(key_list0))
>
>>
>> So I head down the path of all sorts of variations of the following.
>> And I mean all sorts. Instead of [reenter, key_columns] I used a
>single list.
>> Instead of the exec('reenter = True') I used a simple reenter=True
>> (and an
>
>This has become total nonsense to me. There may be something you're
>aiming at, but I think it's a long way from April Fool.
>
>Perhaps you're trying for a Python obfuscation contest. But that's a C
>tradition, not Python. In Python, readability is preferred.
Of course it is an obfuscation. If you can write such code it shows that you
understand it, but understanding and using are two wildly different
concepts. One could easily argue that comprehensions are obfuscating, after
all the long hand code isn't that much longer than the comprehension. But
using the comprehension just makes the code easier to understand. It's kind
of like contractions: 'it is' is the long hand, while "it's" is the
comprehension. When lil tikes are learning to read, they see and use 'it
is', once they become more capable in their comprehension (pun intended),
they can use the shortcut "it's".
>
>> eval and compile which I don't totally understand yet).
>
>"exec" and "eval" ? Run for the woods.
I've received this from just about everybody, I'll conform here:<)
>
>If you like writing obscure code, then you can do it with a list
>comprehension. Just wrap the comprehension in an all() or any() call.
>Don't try to set external variables in an inner scope.
I wasn't, the reenter was used as an internal and the result just happened
to be called reenter through various iterations.
Clayton
>
>Something like (untested):
> get_new_list = any( [ True for item in key_list if item not in
>key_list0] )
>
>This has the disadvantage that it won't stop on the first missing key,
>but will try them all regardless.
>
>In any case, you should probably make two functions, one with the
>reference implementation, and one with the experiment. And your test
>loop should call both of them, compare the results, and indicate when
>there's a discrepancy.
>
>
>
>--
>DaveA
>_______________________________________________
>Tutor maillist - Tutor at python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list