[Python-ideas] Python reviewed

Matthias Bussonnier bussonniermatthias at gmail.com
Mon Jan 9 09:32:49 EST 2017


On Mon, Jan 9, 2017 at 2:50 PM, Simon Lovell <simon58500 at bigpond.com> wrote:
> Hmm, Thanks Chris. I thought I was posting this to the correct place.
>
> I've never seen that "for line in open ..." after googling it many times!
> Why is this question so often asked then?
>

The distinction and the explanation of this is the first result of the
google search

"Python process file line by line"

http://stackoverflow.com/questions/11130312/line-by-line-file-processing-for-loop-vs-with

You probably haven't came across that because you think and search
using C terms.
Once you get used to context managers they are incredibly useful.
I would advise to watch talks like "Beyond Pep 8" [1], comparing the same
program in Python and Java.

>
> Re:Indentation making end block markers not needed; well yes they aren't
> /needed/. However, they are useful for readability purposes. Perhaps if I
> use it some more I'll see that they aren't but I doubt it.

I use to like end markers. Though not having them make the code quite shorted.
When you have 1 line condition, or context manager or loop, it
literally adds 50%
more line to your condition/loop/contextmanager.

As the next line is anyway de-indented, you see your block anyway.

>
>
> Re:Everything being true of false. I don't see the value of that. Only
> boolean data should be valid in boolean contexts. I don't really see how
> that can be argued.
>

Things are true-ish or false-ish, if you prefer. This allows idioms like

if mycontainer:
   process_my_container(mycontainer)

And you can process it only if you have items and it is not none.
Your own object can raise if they get casted to bools, so if you really
like your object to not behave like a bool, you can.

It's not because they are truthy or falsy that they compare equal:

>>> if [] == False:
...     print('[] equals False')
... else:
...     print('[] not equals...')
...
[] not equals...
>>> if not []:print('[] is Falsy')
...
[] is Falsy


Also Boolean in Python are singletons (like None) , so you will see
comparison by identity `... is None` ( `... is False`, `... is True`
rarely) if you really care about only being in boolean context you
can.

Also Python is "ducktyped", if it quack and walks like a duck , then
it's a duck.
If an object defines how to behave as a bool then that's great.
You can then use your own object that are truthy-falsy and carry more
information by still using other libraries.

if not allowed:
   raise OhNoooe('You can't because', allowed.reason)

-- 
M

[1]:https://www.youtube.com/watch?v=wf-BqAjZb8M

Sorry Simon for double Mail, I forgot to reply-all.


More information about the Python-ideas mailing list