6 Replies - 1030 Views - Last Post: 23 August 2018 - 02:18 PM Rate Topic: -----

#1 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11688
  • View blog
  • Posts: 19,857
  • Joined: 19-March 11

What I learned about python today

Posted 09 April 2018 - 10:54 AM

Every now and then, I learn a thing. Here's what I learned today:

Everyone knows that the Right Way to open a file is

with open(filename) as file:
  do_stuff_with(file)



Many people know that the with keyword asks the object to create a contextual scope and to do some cleanup on exiting that scope. (if any of that is new to you, then look up "python context manager", it's pretty neat)

Today, I came across this invocation:

with file as myfile:
  do_stuff_with(myfile)



This threw me for a minute: surely that can't be right? But then I thought about it for a minute and yes, it's right: the file object is the thing that knows about the context, so renaming it in this way also insures that it will close itself on exit. Seems obvious now that I say it aloud, but I'd never thought about it.

So I rewrote that to be even simpler:
with file:
  do_stuff_with(file)



(the reason we don't do the open here is that on one codepath we end up with file=sys.stdin which doesn't get opened)

What did you learn today?

Is This A Good Question/Topic? 1
  • +

Replies To: What I learned about python today

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2996
  • View blog
  • Posts: 11,539
  • Joined: 03-December 12

Re: What I learned about python today

Posted 09 April 2018 - 11:41 AM

I learned the sqlfiddle doesn't like triggers.
Was This Post Helpful? 0
  • +
  • -

#3 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 561
  • View blog
  • Posts: 1,791
  • Joined: 27-December 13

Re: What I learned about python today

Posted 12 August 2018 - 02:29 AM

I just today read PEP 572, - Assignment Expressions. Link: https://www.python.o.../peps/pep-0572/

Traditionally you need to do like this to ensure a single pass through a complex calculation:
x = complex_calculation()
if x > threshold:
    y = f(x)


With assignment expression you can do:
if x := complex_calculation() > threshold:
    y = f(x)


With PEP 572 a new operator := becomes available.
PEP 572 is expected to be implemented with python 3.8 in October 2019
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11688
  • View blog
  • Posts: 19,857
  • Joined: 19-March 11

Re: What I learned about python today

Posted 12 August 2018 - 09:36 AM

Heh. The phrase "expensive operation" gets a new meaning - the argumentation over this operator is why Guido stepped down as BDFL of the Python community.
Was This Post Helpful? 0
  • +
  • -

#5 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 561
  • View blog
  • Posts: 1,791
  • Joined: 27-December 13

Re: What I learned about python today

Posted 12 August 2018 - 10:21 AM

Yeah. I've read about the discussions over the operator and I've read about Guidos 'permanent vacation' from his BDFL-ship. Don't know if there is a direct correlation, though.

As an amateur/hobbyist programmer, I'm not so much into the discussion details of this assignment expression; all I can say is that I certainly have felt the need for such operator many times. The only downside that I see is increased complexity of the language, but this addendum should not flip anyone's day...?

Personally, I see less use of the f-string print option introduced with version 3.6 - this is really a 'nice-to-have' and not a 'need-to-have' operator.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11688
  • View blog
  • Posts: 19,857
  • Joined: 19-March 11

Re: What I learned about python today

Posted 12 August 2018 - 02:32 PM

No doubt, the new operator is going to be very useful. But it's pretty clear that it was the arguments over it that pushed Guido over the edge:

Quote

Now that PEP 572 is done, I don't ever want to have to fight so hard for a
PEP and find that so many people despise my decisions.

I would like to remove myself entirely from the decision process. I'll
still be there for a while as an ordinary core dev, and I'll still be
available to mentor people -- possibly more available. But I'm basically
giving myself a permanent vacation from being BDFL, and you all will be on
your own.


This in no way takes away from the value of the := operator - but it's a valuable reminder that when we have arguments we should remember to keep the big picture in mind.

Probably this could have been settled without this outcome.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11688
  • View blog
  • Posts: 19,857
  • Joined: 19-March 11

Re: What I learned about python today

Posted 23 August 2018 - 02:18 PM

I did not know this:

Quote

The pyperclip module has copy() and paste() functions that can send text
to and receive text from your computerís clipboard.


And now I do.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1