Re: boolean from a function



On 12/13/2011 11:37 PM, Steven D'Aprano wrote:

x is a global? Poor design. But in any case, instead of an explicit
if...else block, the canonical way to convert an arbitrary object to True/
False is with bool:

def func_bool():
return bool(x)

But you don't need it. See below.



No no it was just to show the pattern, it wasn't the actual code.
I don't like to have useless indirections, so I wouldn't do that...

I like the idea of the property (from Duncan Booth) but the thing is that that function
looks like it's doing something (from its name), it's not just a simple property.

In the case of the square

class Sq(object):
def __init__(self, x, y):
self.x = x
self.y


It makes perfect sense to have "area" as a property, because you can either compute
it and cache it or compute it on demand.
It feels a bit less natural to create a property on something that is less simple than that imho..
.



Relevant Pages

  • Re: [OT-ish] Design principles: no bool arguments
    ... Python does not have) versus keyword-optional or keyword only. ... callers always have the 'keyword = BOOL' option. ... def spam_off: ... Note that naming the function 'switch' and putting the bool as ...
    (comp.lang.python)
  • Re: Equivalent code to the bool() built-in function
    ... For that reason alone, bool is useful. ... arbitrary object, but that's not the primary purpose for bool existing. ... Python had no built-in True and False values. ... and they will do arithmetic on those flags; ...
    (comp.lang.python)
  • Passing a method indirectly
    ... I'm trying to write a function that takes an arbitrary object and ... def test1: print "Hello" ... def test2: ...
    (comp.lang.python)
  • Re: Is it ok to type check a boolean argument?
    ... Me and my colleagues are having an discussion about the best way to ... Here is the offending function: ... def find: ... ....if not isinstance(order, bool): ...
    (comp.lang.python)
  • Re: sqlite3 adaptors mystery
    ... if bol: ... def convert_boolean: ... [matej@viklef dumpBugzilla]$ python testAdaptors.py False ... I would get back bool values not strings. ...
    (comp.lang.python)