[Tutor] beginning to code
Rustom Mody
rustompmody at gmail.com
Mon Sep 18 08:54:46 EDT 2017
On Monday, September 18, 2017 at 5:23:49 PM UTC+5:30, Rick Johnson wrote:
> On Sunday, September 17, 2017 at 8:51:38 PM UTC-5, INADA Naoki wrote:
> > >
> > > >
> > > > I would agree that testing any of those for '== True' or
> > > > the like is pointless redundancy,
> > >
> > > But what's wrong with syntactical redundancy when it brings
> > > _clarity_ to the source code? And why can't Python be smart?
> > > Consider the collowing code:
> > >
> > > if bool(someObject) == True:
> > > # Do something
> > >
> > > Yes, from a "byte-code perspective", this source code is
> > > superfluous, but, from a source code perspective, this code
> > > is perfectly balanced between explicit and implicit.
> >
> > I can't agree with you. It's too redundant. It doesn't
> > provide any information about what coder think.
>
> It's not about what the "coder thinks", many times what the
> coder thinks is wrong, it's about writing code that is
> intuitive to as wide an audience as possible.
>
> > While PEP 8 recommends `if x:`, I accept `if x > 0` or `if
> > len(x) > 0` when I review code in my company.
>
> So when `x` is an iterable, as in:
>
> if len(x) > 0:
> # blah
>
> You're okay with the explicit test. Or when `x` is a
> numeric, as in:
>
> if x > 0:
> # blah
>
> You're okay with the explicit test. So, from a standpoint of
> consistency, you /should/ be okay with this:
>
> if bool(x) == True:
> # blah
>
> But you're not! :-).
I have a feeling Rick that you are mixing up two unrelated things:
- the bool(x) part
- the ... == True part
The operation
x == True
for true(!)/proper booleans x is a no-op
because True == True is True
and False == True is False
And there are no other (proper) booleans
However because anything else can be bool-ish even though not boolean
you need the bool(x) to effect the mapping:
{None, 0, "" {}, []} → False
Everything_else → True
This mapping is neither obvious nor trivial
And one could argue that leaving python to implicitly make [] (say) into False
should be documented
So if you drop the hangup with the red-herring ...==True
you have a point in asking for the bool(...)
More information about the Python-list
mailing list