Re: create boolean

2009-03-09 Thread Andre Engels
On Mon, Mar 9, 2009 at 12:48 AM, Grant Edwards gra...@visi.com wrote:

 I didn't say that he hadn't authorized that assumption.  I just
 said that the code does rely on such an assumption.  In my
 experience, assumptions like that result broken code down the
 road.

And assumptions like when assumptions fail, it is save to go by the
letter of the requirement don't?

-- 
André Engels, andreeng...@gmail.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: create boolean

2009-03-09 Thread Steve Holden
Lie Ryan wrote:
 Fencer wrote:
 Hi, I need a boolean b to be true if the variable n is not None and
 not an empty list, otherwise b should be false.
 I ended up with:
 b = n is not None and not not n
 which seems to work but is that normally how you would do it?
 It can be assumed that n is always None or a list that might be empty

 - Fencer
 
 The literal translation of that would be:
 
 if n is not None and n != []:
 b = True
 else:
 b = False
 
 it is a bit verbose, so one might want to find something shorter
 
 b = True if n is not None and n != [] else False
 
 I always feel if and in-line if to be easier and more readable than
 short-circuited operations.

So, in what significant way does this differ from

  b = n is not None and n != []

? Just for kicks, let's take a look at that:

# py2.2+ ... not py3 ?1.5.2? ?1.4?
print %7s %7s %7s %7s %7s %7s % (
  a, b, bool(a), bool(b), formula1, formula2)

formula1 = True if n is not None and n != [] else False
formula2 = n is not None and n != []
print 64 * -

for a in None, [], 0, 33, ,  banana, {}, {1: 'one'}:
for b in -:
n = a
print %7s %7s %7s %7s %7s %7s % (
a, b, bool(a), bool(b),
eval(formula1), eval(formula2))

Those who run the program may see the results. I hope the small attached
graphic will be acceptable to them so those with no interpreter handy
may also see the results. If you do have an interpreter you are, of
course, free to vary the formulae, or add more for comparison.

regards
 Steve

PS: Indeed b *is* completely redundant. I just adapted a program I had
lying around ...
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Want to know? Come to PyCon - soon! http://us.pycon.org/
inline: bool.png--
http://mail.python.org/mailman/listinfo/python-list


Re: create boolean

2009-03-09 Thread Grant Edwards
On 2009-03-09, Andre Engels andreeng...@gmail.com wrote:
 On Mon, Mar 9, 2009 at 12:48 AM, Grant Edwards gra...@visi.com wrote:

 I didn't say that he hadn't authorized that assumption. ??I
 just said that the code does rely on such an assumption. ??In
 my experience, assumptions like that result broken code down
 the road.

 And assumptions like when assumptions fail, it is save to go by the
 letter of the requirement don't?

If the requirement is wrong, then it doesn't really matter what
you assume.

-- 
Grant Edwards   grante Yow! OVER the underpass!
  at   UNDER the overpass!
   visi.comAround the FUTURE and
   BEYOND REPAIR!!
--
http://mail.python.org/mailman/listinfo/python-list


Re: create boolean

2009-03-08 Thread Lie Ryan

Scott David Daniels wrote:

Lie Ryan wrote:

Fencer wrote:
The literal translation of that would be:
if n is not None and n != []:
b = True
else:
b = False
it is a bit verbose, so one might want to find something shorter
b = True if n is not None and n != [] else False
I always feel if and in-line if to be easier and more readable than 
short-circuited operations.


How about:
   b = None is not n != []

It is amazing to think about how rarely we consider is / is not as
a comparison operator.  Also, and more reasonably, we often don't
consider chaining comparisons that are intransitive.


The fact that chaining operator is possible doesn't mean it must be 
used. The only place I would use chained operator is for comparing 
ranges of integer: 5  x  10, other than that, its use often reduce 
readability.

--
http://mail.python.org/mailman/listinfo/python-list


Re: create boolean

2009-03-08 Thread Rhodri James

On Sat, 07 Mar 2009 05:03:08 -, Grant Edwards gra...@visi.com wrote:


On 2009-03-07, Rhodri James rho...@wildebst.demon.co.uk wrote:
On Fri, 06 Mar 2009 15:34:08 -, Grant Edwards inva...@invalid  
wrote:



On 2009-03-06, Fencer no.s...@plz.ok wrote:


Hi, I need a boolean b to be true if the variable n is not
None and not an empty list, otherwise b should be false.



I ended up with:



b = n is not None and not not n


I'd do it like this:

  b = (n is not None) and (n != [])


The second comparison isn't actually necessary, since an
empty list is True and a non-empty one False.

   b = (n is not None) and n

Putting the comparison in does make the code slightly less
magic, though, so it's not a bad idea to do it!


Putting in the second comparison in makes the code match the
stated requirement.  Otherwise you have to start making
assumptions about what n might be besides None or the empty
list.


The OP stated that we *could* assume that n was None or a
list, so I stand by what I said.

--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: create boolean

2009-03-08 Thread Grant Edwards
On 2009-03-08, Rhodri James rho...@wildebst.demon.co.uk wrote:

   b = (n is not None) and (n != [])

 The second comparison isn't actually necessary, since an
 empty list is True and a non-empty one False.

b = (n is not None) and n

 Putting the comparison in does make the code slightly less
 magic, though, so it's not a bad idea to do it!

 Putting in the second comparison in makes the code match the
 stated requirement.  Otherwise you have to start making
 assumptions about what n might be besides None or the empty
 list.

 The OP stated that we *could* assume that n was None or a
 list, so I stand by what I said.

I didn't say that he hadn't authorized that assumption.  I just
said that the code does rely on such an assumption.  In my
experience, assumptions like that result broken code down the
road.

-- 
Grant

--
http://mail.python.org/mailman/listinfo/python-list


Re: create boolean

2009-03-07 Thread Lie Ryan

Fencer wrote:
Hi, I need a boolean b to be true if the variable n is not None and not 
an empty list, otherwise b should be false.

I ended up with:
b = n is not None and not not n
which seems to work but is that normally how you would do it?
It can be assumed that n is always None or a list that might be empty

- Fencer


The literal translation of that would be:

if n is not None and n != []:
b = True
else:
b = False

it is a bit verbose, so one might want to find something shorter

b = True if n is not None and n != [] else False

I always feel if and in-line if to be easier and more readable than 
short-circuited operations.

--
http://mail.python.org/mailman/listinfo/python-list


Re: create boolean

2009-03-07 Thread Paul Rubin
Fencer no.s...@plz.ok writes:
 Hi, I need a boolean b to be true if the variable n is not None and
 not an empty list, otherwise b should be false
 It can be assumed that n is always None or a list that might be empty

b = bool(n)
--
http://mail.python.org/mailman/listinfo/python-list


Re: create boolean

2009-03-07 Thread Scott David Daniels

Lie Ryan wrote:

Fencer wrote:
The literal translation of that would be:
if n is not None and n != []:
b = True
else:
b = False
it is a bit verbose, so one might want to find something shorter
b = True if n is not None and n != [] else False
I always feel if and in-line if to be easier and more readable than 
short-circuited operations.


How about:
   b = None is not n != []

It is amazing to think about how rarely we consider is / is not as
a comparison operator.  Also, and more reasonably, we often don't
consider chaining comparisons that are intransitive.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: create boolean

2009-03-07 Thread Andre Engels
On Sat, Mar 7, 2009 at 6:03 AM, Grant Edwards gra...@visi.com wrote:

 Putting in the second comparison in makes the code match the
 stated requirement.  Otherwise you have to start making
 assumptions about what n might be besides None or the empty
 list.

But the stated requirement already assumes that n is either None or a
list. The outcome is simply undefined when used on something that is
not None or a list. And it feels more in line with Python philosophy,
in particular with duck typing, to have 'list-like objects' (like sets
or tuples) behave like lists.

-- 
André Engels, andreeng...@gmail.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: create boolean

2009-03-06 Thread Christian Heimes
Fencer schrieb:
 Hi, I need a boolean b to be true if the variable n is not None and not
 an empty list, otherwise b should be false.
 I ended up with:
 b = n is not None and not not n
 which seems to work but is that normally how you would do it?
 It can be assumed that n is always None or a list that might be empty

b = bool(n)

Christian

--
http://mail.python.org/mailman/listinfo/python-list


Re: create boolean

2009-03-06 Thread Grant Edwards
On 2009-03-06, Fencer no.s...@plz.ok wrote:

 Hi, I need a boolean b to be true if the variable n is not
 None and not an empty list, otherwise b should be false.

 I ended up with:

 b = n is not None and not not n

I'd do it like this:

  b = (n is not None) and (n != [])

Your code doesn't meet your stated requirement.  Your code
incorrectly evaluates to False if n is any of the following:

 (,)
 {}
 False
 0
 0.0
 
Your stated requirement is for b to be True for all those cases. 

 which seems to work

 but is that normally how you would do it?

 It can be assumed that n is always None or a list that might
 be empty

Well then, that's different. :)

Still, I'd do it my way.  I don't like assumptions.
  
-- 
Grant Edwards   grante Yow! If Robert Di Niro
  at   assassinates Walter Slezak,
   visi.comwill Jodie Foster marry
   Bonzo??
--
http://mail.python.org/mailman/listinfo/python-list


Re: create boolean

2009-03-06 Thread Rhodri James

On Fri, 06 Mar 2009 15:34:08 -, Grant Edwards inva...@invalid wrote:


On 2009-03-06, Fencer no.s...@plz.ok wrote:


Hi, I need a boolean b to be true if the variable n is not
None and not an empty list, otherwise b should be false.



I ended up with:



b = n is not None and not not n


I'd do it like this:

  b = (n is not None) and (n != [])


The second comparison isn't actually necessary, since an
empty list is True and a non-empty one False.

  b = (n is not None) and n

Putting the comparison in does make the code slightly less
magic, though, so it's not a bad idea to do it!


--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: create boolean

2009-03-06 Thread Grant Edwards
On 2009-03-07, Rhodri James rho...@wildebst.demon.co.uk wrote:
 On Fri, 06 Mar 2009 15:34:08 -, Grant Edwards inva...@invalid wrote:

 On 2009-03-06, Fencer no.s...@plz.ok wrote:

 Hi, I need a boolean b to be true if the variable n is not
 None and not an empty list, otherwise b should be false.

 I ended up with:

 b = n is not None and not not n

 I'd do it like this:

   b = (n is not None) and (n != [])

 The second comparison isn't actually necessary, since an
 empty list is True and a non-empty one False.

b = (n is not None) and n

 Putting the comparison in does make the code slightly less
 magic, though, so it's not a bad idea to do it!

Putting in the second comparison in makes the code match the
stated requirement.  Otherwise you have to start making
assumptions about what n might be besides None or the empty
list.

-- 
Grant

--
http://mail.python.org/mailman/listinfo/python-list