Quantcast

Truth Tests (bool)

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Truth Tests (bool)

Ganesh Kumar-13
Hi Guys,

>>> 3 and 4, [3, 4] and []
      (4, [])
>>> 3 and 4
4

How to compare value in logical operation

empty list [] = False
How to and operation working, please guide me guys.

Did I learn something today? If not, I wasted it.
_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Truth Tests (bool)

Senthil Kumaran-7
They are called Python Logic Short Circuits.

A good way to remember this is here

http://uthcode.appspot.com/blog/2011/07/Python-Logic-short-circuits#comment-266357377

For your other question, an empty list is always fast.

--
Senthil

On Tue, Jan 10, 2012 at 10:28:43PM +0530, Ganesh Kumar wrote:
>
> Did I learn something today? If not, I wasted it.

(Don't be so hard on yourself. :) )
_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Truth Tests (bool)

Gora Mohanty-5
In reply to this post by Ganesh Kumar-13
On Tue, Jan 10, 2012 at 10:28 PM, Ganesh Kumar <[hidden email]> wrote:

> Hi Guys,
>
>>>> 3 and 4, [3, 4] and []
>      (4, [])
>>>> 3 and 4
> 4
>
> How to compare value in logical operation
>
> empty list [] = False
> How to and operation working, please guide me guys.

Google turns up a few million links (literally) for
"Python truth test". Take a look at
http://python.about.com/od/pythonstandardlibrary/p/truth_values.htm
for example.

It is not clear what you want from your description.
To check if a list is empty, see if len( list ) is zero.

Regards,
Gora
_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Truth Tests (bool)

Senthil Kumaran-7
On Tue, Jan 10, 2012 at 10:55:58PM +0530, Gora Mohanty wrote:
> To check if a list is empty, see if len( list ) is zero.

I think, usually we just test the empty list for it's False-ness.
Isn't it?

--
Senthil

_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Truth Tests (bool)

Ganesh Kumar-13
HI.


> I think, usually we just test the empty list for it's False-ness.
> Isn't it?
>
> In [8]: x = []

In [9]: bool(x)
Out[9]: False


-Ganesh.
_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Truth Tests (bool)

Noufal Ibrahim
In reply to this post by Gora Mohanty-5
Gora Mohanty <[hidden email]> writes:


[...]

> It is not clear what you want from your description.  To check if a
> list is empty, see if len( list ) is zero.

[...]

You don't want to do that. Your "list" might be a generator (unless you
check for type which is a bad idea anyway) and "len"ing that will
consume it which might be a potentially expensive operation.

Truth testing in general is implemted using the __nonzero__ magic
method[1].

Lists don't have a __nonzero__ so the truth value is determined using
the length (as you've said) but manually doing it on something that you
don't know the type of is not usually a good idea.

Footnotes:
[1]  http://docs.python.org/reference/datamodel.html#object.__nonzero__

--
~noufal
http://nibrahim.net.in

May I ask a question?
_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Truth Tests (bool)

Saager Mhatre
https://gist.github.com/1327614#file_truth.py

I hacked together this little script to cover (hopefully) all the cases for
my reference.

- d
_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Truth Tests (bool)

Senthil Kumaran-7
In reply to this post by Noufal Ibrahim
On Wed, Jan 11, 2012 at 09:49:58AM +0530, Noufal Ibrahim wrote:

> > It is not clear what you want from your description.  To check if a
> > list is empty, see if len( list ) is zero.
>
> You don't want to do that. Your "list" might be a generator (unless you
> check for type which is a bad idea anyway) and "len"ing that will
> consume it which might be a potentially expensive operation.

While the explaination on __nonzero__ is useful out of this context,
how can list be a generator? I am assuming that none are confusing the
terminologies.

In practical cases for testing boolean in lists, just use the list as
the test. Empty list is false.

--
Senthil

_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Truth Tests (bool)

Senthil Kumaran-7
In reply to this post by Saager Mhatre
On Wed, Jan 11, 2012 at 03:14:05PM +0530, Saager Mhatre wrote:
> https://gist.github.com/1327614#file_truth.py

> I hacked together this little script to cover (hopefully) all the cases for
> my reference.

Cool. Good to see some difficult to comprehend code. :)

The bool's are intuitively easier to remember in Python.

--
Senthil
_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Truth Tests (bool)

Noufal Ibrahim
In reply to this post by Senthil Kumaran-7
Senthil Kumaran <[hidden email]> writes:

> On Wed, Jan 11, 2012 at 09:49:58AM +0530, Noufal Ibrahim wrote:
>
>> > It is not clear what you want from your description.  To check if a
>> > list is empty, see if len( list ) is zero.
>>
>> You don't want to do that. Your "list" might be a generator (unless you
>> check for type which is a bad idea anyway) and "len"ing that will
>> consume it which might be a potentially expensive operation.
>
> While the explaination on __nonzero__ is useful out of this context,
> how can list be a generator? I am assuming that none are confusing the
> terminologies.

A list can't be a generator but if you write a function to receive an
iterable x and then do a len(x) to check it it's True or False, it will
work fine for lists but passing a generator this function might have
unpredictable results. That's why I'm cautioning against using `len` to
check for whether an object is empty or not.

> In practical cases for testing boolean in lists, just use the list as
> the test. Empty list is false.

Agreed. This is the right way to do it rather than using len.

--
~noufal
http://nibrahim.net.in

Why don't you pair `em up in threes? -Yogi Berra
_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Truth Tests (bool)

Saager Mhatre
In reply to this post by Noufal Ibrahim
On Wed, Jan 11, 2012 at 9:49 AM, Noufal Ibrahim <[hidden email]>wrote:

> Gora Mohanty <[hidden email]> writes:
>
>
> [...]
>
> > It is not clear what you want from your description.  To check if a
> > list is empty, see if len( list ) is zero.
>
> [...]
>
> You don't want to do that. Your "list" might be a generator (unless you
> check for type which is a bad idea anyway) and "len"ing that will
> consume it which might be a potentially expensive operation.


Noufal,
Noticed something interesting as I was updating
truth.py<https://gist.github.com/1327614#file_truth.py> to
include generators and generator expressions; the `len` function doesn't
handle generators at all.

$ *python -c 'len(i for i in range(10))'*
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: object of type 'generator' has no len()


I'm pretty sure you just meant 'iterable' where you wrote 'generator', or
didn't you?

- d
_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Truth Tests (bool)

Sreenivas Reddy T
I would like to clear things here.I am assuming beginner as  a common
denominator to this post.

len() method is  just a shortcut to __len__ magic method .So when you  call
len() on your own container type ,it does not traverse whole sequence ,it
just calls __len__ method and returns value(assume self.size=0 in
constructor) from this method.This value incremented or decremented in  add
and remove method on your container type.

Moreover,there can never be length method on iterators - (generators are
also iterators,not vice versa).Iterators are meant for traversing a
sequence or container,they do not keep track of length..generators are
meant to produce infinite items.

When you call len(list) or len(deque) or any collection type,they override
this __len__ internally or some other similar mechanism .That is very much
“similar” to ArrayList here in java.

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/ArrayList.java

the same can be seen here ..

http://hg.python.org/cpython/file/ba975c7c33d3/Modules/_collectionsmodule.c

To make your type iterable ,you just have to implement __iter__ and next()
method,there is  no __len__ method here.So you will always receive " there
is no len() method your object method".Since you passed generator object to
it.you received that error.

This whole discussion is equal to IEnumerable<T> and ICollection<T> in C#.

python gist is here ,https://gist.github.com/1607183

anybody can test it.

Thanks & Regards,
Sreenivas Reddy Thatiparthy,
9703888668.

"Anyone who has never made a mistake has never tried anything new !!! "
--Albert Einstein
_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Truth Tests (bool)

Noufal Ibrahim
In reply to this post by Saager Mhatre
Saager Mhatre <[hidden email]> writes:


[...]

> I'm pretty sure you just meant 'iterable' where you wrote 'generator',
> or didn't you?

[...]

Yes. I did. It was a slip up.


--
~noufal
http://nibrahim.net.in

I'm proud of my humility.
_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Truth Tests (bool)

Noufal Ibrahim
In reply to this post by Sreenivas Reddy T

Sreenivas Reddy T <[hidden email]> writes:

> I would like to clear things here.I am assuming beginner as  a common
> denominator to this post.
>
> len() method is just a shortcut to __len__ magic method .So when you
> call len() on your own container type ,it does not traverse whole
> sequence ,it just calls __len__ method and returns value(assume
> self.size=0 in constructor) from this method.This value incremented or
> decremented in add and remove method on your container type.

This is an interesting point especially with Python3 returning something
other than lists for things like dict.keys. I'll dig into this a
little.

Thanks.

[...]


--
~noufal
http://nibrahim.net.in

Honk if you are against noise pollution!
_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Truth Tests (bool)

Senthil Kumaran-7
On Sat, Jan 14, 2012 at 12:20:11AM +0530, Noufal Ibrahim wrote:
> > len() method is just a shortcut to __len__ magic method .So when you
> > call len() on your own container type ,it does not traverse whole
> > sequence ,it just calls __len__ method and returns value(assume
> > self.size=0 in constructor) from this method.This value incremented or
> > decremented in add and remove method on your container type.
>
> This is an interesting point especially with Python3 returning something
> other than lists for things like dict.keys. I'll dig into this a
> little.

Yeah, dict view or in general the concept of memoryview, but they do
have len().

--
Senthil
_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Truth Tests (bool)

Sreenivas Reddy T
In reply to this post by Noufal Ibrahim
>
>
> This is an interesting point especially with Python3 returning something
> other than lists for things like dict.keys. I'll dig into this a
> little.
>
> Thanks.
>
>
Noufal,if haven't gone through ,

>From the PEP -3106.
"This PEP proposes to change the .keys(), .values() and .items() methods of
the built-in dict type to return a set-like or unordered container object
whose contents are derived from the underlying dictionary rather than a
list which is a copy of the keys, etc.; and to remove the .iterkeys(),
.itervalues() and .iteritems() methods."

http://www.python.org/dev/peps/pep-3106/

Please see the pseudo-code  at the bottom,they override __len__ magic
method.
_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Loading...