[Tutor] not operator

W W srilyk at gmail.com
Fri Sep 19 12:34:21 CEST 2008


On Fri, Sep 19, 2008 at 4:49 AM, Paul McGuire <ptmcg at austin.rr.com> wrote:

> because validate, being a transitive verb, tells us we are going to do
> something to config, but since 'validate' is not a name that clearly
> asserts
> a truth or falsity, we aren't exactly sure what validate is going to return
> for valid vs. invalid configs (I could envision a function named 'validate'
> could return a list of validation errors, for example).  A better name here
> is 'is_valid', so that the '== True' can just be dropped, and the code
> reads
> clearly as:
>
> if config.is_valid():
>
> </soapbox>
> -- Paul
>
> * yes, I know this phrase is itself multiply redundant - sometimes I just
> crack myself up.


Actually, I tend to work this same way myself (though I'm currently taking
C++ at school, since they don't offer any serious python classes *tear*) -
when I'm sorting an array in C++ (of course in python this is simply a
method) I create a boolean value called "sorted" and give it the value
False. If you were to translate my code to python it would look a bit like
this:

sorted = False
while not sorted:
    swap values
    if swaps <= 0:
        sorted = True

Which makes it read a lot more naturally. The same for validating input. In
python you could write:

valid_input = False
while not valid_input:
    my_input = raw_input("Enter something: ")
    if my_input in ('my', 'criteria'):
        valid_input = True

It's worked for me so far ;)

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080919/a879727c/attachment.htm>


More information about the Tutor mailing list