[Tutor] all right students, what do we learn

Dave Angel d at davea.name
Sun Nov 2 22:49:05 CET 2014


On 11/02/2014 03:05 PM, Clayton Kirkwood wrote:
>
>
>> -----Original Message-----
>> From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On
>> Behalf Of Alan Gauld
>> Sent: Sunday, November 02, 2014 1:04 AM
>> To: tutor at python.org
>> Subject: Re: [Tutor] all right students, what do we learn
>>
>> On 02/11/14 05:43, Clayton Kirkwood wrote:
>>
>>> 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 eval and compile which I don't totally understand
>> yet).
>>
>> Don't use exec, eval or compile. Consider them for experts only and
>> hardly ever needed.
>>
>> They are extremely dangerous and doubly so if you don't know exactly
>> what you are doing. You could, for example, wind up accidentally
>> deleting your files or formatting your hard disk.
>
>
> Got it. I'll wait until next month:<}}}
>
>>
>>> out that python doesn't seem to like an assignment in the overall if
>>> clause - error.
>>
>> Correct, the if statement requires an expression (specifically a boolean
>> expression). And assignments are not expressions.
>
> My thinking was that I was making a Boolean value:
>
>
> ]=[(reenter, key) for key in key_list if
>>> ((key in key_list0)) or (print("Error:", key, "not available, start
>>> again") and exec('reenter = True', ))]
>
>
> In the first case, key is in key_list0 so that is a True and we are finished
> for that key. In the other case, key is not in key_list0 so we go on past
> the or. I print which returns a non-False, ergo a True,

But it doesn't.  print() returns None, which is false.

> and then I make an
> assignment which should be a True. This was I am able to set reenter to
> True. Assignments create a Boolean True (or should).

Assignment is a type of statement.  There's nothing created.  The lhs is 
bound to the rhs.  And the "=" is NOT an operator, and may not be used 
in an expression.  Python is not C.

If you're trying to hack an expression with side effect, perhaps you 
should write a function:

def force():
     global reenter
     reenter = True
     return True



>
> The other issue is that I had the outside reenter set as true to start the
> for and turn it False so that the for won't re-occur unless I have a bad key
> and force reenter to be a True.
>
>
>>
>>> Weird thing, entering   a y   would make the final
>>> print spit out (False, 'a') (False, 'o') but the entry of  hhh
>> caused
>>> the final print to spit out []
>>
>> Why do you think that weird? Its exactly what I'd expect. You don't have
>> a key 'hhh' in the data you showed us.
>>
>> Remember that the comprehension is trying to build a list containing
>> only those output expressions that match the if clause. If the if fails
>> then nothing gets put in the output.
>
> Again, the code, if the equal sign worked would always force a True. In one
> case reenter doesn't change, in the second case, reenter does change but the
> expression is still True.
>
>

It could be, if you rechecked your assumptions, such as the return value 
of print()

>>
>>> Which I don't quite understand. The reason is because I set up the if
>>> statement to be True either way. With an   a   it would go thru and
>>> create the proper output.
>>
>> Can you explain in English what you want the output to be?
>> The reason I ask is that you seem to be creating a list of pairs where
>> the first element is the result of the 'in' test and the second is the
>> value. Do you really want that or do you just want the ones that match
>> either true or false?
>
> What I would expect from [('a', 'apple'), ('b', 'banana')] and entering a y
> Would be:
> [('a',True), ('y', False)]

But "a" isn't in the list [('a', 'apple'), ('b', 'banana')].  Maybe 
you'd better get the basics right before you try to get obfuscated with 
your code.  Write real data for a real function that contains a real 
loop that produces what you really want.  Then you can start with the 
obfuscation.



-- 
DaveA


More information about the Tutor mailing list