osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tutor] beginning to code


Op 19-09-17 om 09:46 schreef Chris Angelico:
> On Tue, Sep 19, 2017 at 5:22 PM, Antoon Pardon <antoon.pardon at vub.be> wrote:
>> But the problem is that the following two pieces of code don't do
>> the same in Python.
>>
>> if x: pass
>> if x is True: pass
>>
> ...
>
> which would be better represented with "if isinstance(x, bool):"
> instead. So "if x is True:" remains odd code, the sort of thing that
> MUST be annotated or I would be raising a red flag. Can you give an
> actual real-world example where this comes up?

The time I used it was when I had some class that the user had to
somehow activate. An instance could internally be in three states
but the user had only access to two, he had activated it or not.
The internal "active" attribute could be:

1) False: The user hasn't activated it.
2) True: The user has activated it, but there was no active background machinery yet/anymore.
3) A handle to the active background machinery.

Maybe I should have used an internal "state" attribute and an enum
with the values unactivated and activated instead but the use of
False and True in the circumstances above felt rather natural to me.

-- 
Antoon Pardon.