Fedora Linux Support Community & Resources Center
  #1  
Old 17th May 2012, 01:17 AM
marko Offline
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 6,027
linuxfirefox
Confused about Python all() function

I've got two classes I'm testing to learn about how to use any() and all() with my own classes. Basically I have a class called Wrapped that has a member "self.members" which is a python list of class instances of another class called Unit. I've set up Unit so it defines a function called __bool__(self) like this:

Code:
    def __bool__(self):
        print "__bool__", self.a   # simply here to confirm __bool__ is called by all()
        if self.a is None:
            return False
        else:
            return True
basically "self.a" in a Unit is just going to be an integer or it could be a None if the Unit() constructor was run with no argument. So this is the Unit class:

Code:
class Unit(object):
    def __init__(self, a=None):
        self.a = a

    def __eq__(self, other):
        return self.a == other.a
 
    def __lt__(self, other):
        return self.a < other.a
 
    # any Unit that had a valid 'a' value set is considered good
    def __bool__(self):
        print "__bool__", self.a       # check any() and all()
        if self.a is None:
            return False
        else:
            return True
and the Wrapped class is:

Code:
class Wrapped(object):
    def __init__(self, mem=None):
        if mem is None:
            self.members = []
        else:
            self.members = [mem]

    def add_member(self, mem=None):
        if mem is not None:
            self.members.append(mem)

    def __len__(self):
        return len(self.members)
        
    def __bool__(self):
        print "Wrapped __bool__"
        if self.members:
            return True
        else:
            return False

    def __str__(self):
        return ''.join([ s.__str__() + ' ' for s in self.members ])
So in a Unit instance with self.a of None case the __bool__() is False otherwise it's simply True (even a self.a of 0 will be True)

When I create a Wrapper instance and load it up with Units I have something unexpected:
Code:
foo = Wrapper()
foo.add_member(Unit(3))
foo.add_member(Unit())
foo.add_member(Unit(12))
print foo
print "all: " + str(all(foo.members))
The "print foo" shows what I expect via the __str__() in my Wrapped class :
Quote:
Unit() 3 Unit() None Unit() 12
But the "print "all: " + str(all(foo.members))" comes up unexpectedly:
Quote:
all: True
I would have expected all(foo.members) to be False since that 2nd class had a None for the "a", plus I also see that the all() doesn't seem to call the Unit.__bool__() anyway because my test "print" doesn't show. I was thinking the all() function always has to use __bool__ from the class in the list?

Last edited by marko; 17th May 2012 at 01:20 AM.
Reply With Quote
  #2  
Old 17th May 2012, 06:16 AM
aleph Offline
Banned (for/from) behaving just like everybody else!
 
Join Date: Jul 2007
Location: Nanjing, China
Posts: 1,332
linuxfirefox
Re: Confused about Python all() function

You cannot overload the bool() function in Python 2. The __bool__ special method is a Python 3 feature. In Python 2 it is nothing special hence useless in your code, which is why it's never called.

To run your code either use python3 (and you must modify your program to deal with the new grammar, in particular the print() function. The 2to3 tool can help you in this regard), or use the __nonzero__ special method.
__________________
Code:
from rlyeh import cthulhu
cthulhu.fhtagn()
Reply With Quote
  #3  
Old 17th May 2012, 06:19 AM
marko Offline
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 6,027
linuxfirefox
Re: Confused about Python all() function

Quote:
Originally Posted by aleph View Post
You cannot overload the bool() function in Python 2. The __bool__ special method is a Python 3 feature. In Python 2 it is nothing special hence useless in your code, which is why it's never called.

To run your code either use python3 (and you must modify your program to deal with the new grammar, in particular the print() function. The 2to3 tool can help you in this regard), or use the __nonzero__ special method.

Wow, so in python 2.x you can't use all() or any() with user defined class lists but only lists using official python objects? It's possible the book I'm using has enough Python 3.x info in it I got mixed up
Reply With Quote
  #4  
Old 17th May 2012, 06:22 AM
aleph Offline
Banned (for/from) behaving just like everybody else!
 
Join Date: Jul 2007
Location: Nanjing, China
Posts: 1,332
linuxfirefox
Re: Confused about Python all() function

Quote:
Originally Posted by marko View Post
Wow, so in python 2.x you can't use all() or any() with user defined class lists but only lists using official python objects? It's possible the book I'm using has enough Python 3.x info in it I got mixed up
You just have to use __nonzero__ instead of __bool__ in Python 2. After you make this change, all() and any() should work as expected.
__________________
Code:
from rlyeh import cthulhu
cthulhu.fhtagn()
Reply With Quote
  #5  
Old 18th May 2012, 03:28 AM
marko Offline
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 6,027
linuxfirefox
Re: Confused about Python all() function

Quote:
Originally Posted by aleph View Post
You just have to use __nonzero__ instead of __bool__ in Python 2. After you make this change, all() and any() should work as expected.
@aleph

Thanks, that worked
Reply With Quote
Reply

Tags
confused, function, python

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
extending python with type whose member is a pointer to a function CowTux Programming & Packaging 0 28th July 2010 11:34 AM
Python, IDLE, Python-tools Gnome. Deonis Using Fedora 2 12th March 2009 03:44 AM
Processing Conflict: python-devel conflicts python< 2.3.4-13.1 guarriman Using Fedora 0 15th September 2005 05:53 PM
creating custom linux distro:confused: :confused: :confused: noobiedora Using Fedora 2 27th February 2005 03:25 PM


Current GMT-time: 09:27 (Saturday, 23-08-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat