Is this pythonic?

Frank Millman frank at chagford.com
Wed Nov 23 23:55:52 EST 2016


"Steve D'Aprano"  wrote in message 
news:583653bb$0$1603$c3e8da3$5496439d at news.astraweb.com...

> Even if the computation of the memoised value is done asynchronously, you
> can easily split the computation off to a separate method (as you already
> talked about doing!) and make getval() block until it returns.

Surely that defeats the whole purpose of asyncio. Anything that blocks holds 
up the entire process. I strenuously try to avoid blocking in any shape or 
form.

> > I can say 'print(await obj.__str__())', and it works, but I lose the
> > ability to include it in a larger print statement.

> Any time you find yourself directly calling dunder methods, you're 
> probably
> doing it wrong. This is one of those times.

Yes. Having slept on it, I realise I over-reacted.

The __str__() method is convenient for me, but I only use it for testing and 
debugging to see what is going on. It is not part of my app per se.

I now realise that the solution is -

1. Keep the __str__ method, but replace calls to getval() with a direct 
reference to the underlying attribute. It means that any 'computable' 
objects that have not already been computed will return None, but that is ok 
for my purposes.

2. Write a separate method, retaining the calls to getval(), to be called 
independently using 'await' if I ever need to see the full result after 
computation.

Frank





More information about the Python-list mailing list