Is this pythonic?

Frank Millman frank at chagford.com
Wed Nov 23 06:11:53 EST 2016


"Marko Rauhamaa"  wrote in message news:87inrer0dl.fsf at elektro.pacujo.net...

"Frank Millman" <frank at chagford.com>:
>
> > 3. When instantiating an object, check if it would need computation -
> >    if computation_required:
> >        self.getval = self._getval_with_comp
> >    else:
> >        self.getval = self._getval
> >
> > 4. In _getval_with_comp, perform the computation, then add the 
> > following -
> >        self.getval = self._getval
> >        return self._getval()
> >
> > What is the verdict? -1, 0, or +1?
>
> Perfectly cromulent, run-of-the-mill Python code.
>

Gah! The law of unintended consequences strikes again!

As I mentioned, the class in question represents a database column. A 
separate class represents a database row. I have a __str__() method on the 
'row' class that prints a nicely formatted representation of the object with 
all of its column objects and their values.

With the above changes, I had to turn getval() into a coroutine. My 
__str__() method uses getval() to obtain the values, so I had to prefix 
getval() with 'await', but then I get a syntax error on __str__(). I can add 
'async' to remove the syntax error, but then print(obj) does not work - 
TypeError: __str__ returned non-string (type coroutine)

I don't think there is an answer to this, but any suggestions will be 
appreciated.

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

Ah well :-(

Frank





More information about the Python-list mailing list