Anders J. Munch wrote:
Ethan Furman wrote:

 > Even if we find out that C.__nonzero__ is called, what was it that
 > __nonzero__ did again?

reinforce the impression that he is unaware of the double-underscore functions and what they do and how they work.


Only if your newsreader malfunctioned and refused to let you read the rest of the paragraph.

Of course I know what __nonzero__ does.

regards, Anders

Anders, my apologies. Since reading that post I came across some other replies you made and realized quite cleary that you do know what you're doing.

By way of explanation as to why I was confused: the "even if we find out" suggested to me that you weren't aware of the calling pattern for "if x" (__nonzero__ if defined, otherwise __len__ if defined). Also, in your sample code you defined a class C, then assigned the variable c using "c=get_a_C()", and get_a_C was never defined. Then you posed the question, "if get_a_C might ever return None"... who knows? You never said what get_a_C did; furthermore, whether or not get_a_C returns a C object or None is completely irrelevant to whether or not __nonzero__ is defined or attribute_is_nonnegative is defined.

FWIW, I completely agree with you as far as naming functions that deal with less consequential attributes; for an attribute that is central to the object, I agree with using __nonzero__. For example:
--------------------------------------------------------------------
class Human(object):
    def __init__(self, hair, eye, skin):
        self.alive = True
        self.haircolor = hair
        self.eyecolor = eye
        self.skincolor = skin
    def __nonzero__(self):
        return self.alive
    def hair_color_is_red(self):
        return self.haircolor in ['red', 'strawberry blonde', 'auburn']
    def eye_color_is_green(self):
        return self.eyecolor in ['green', 'hazel', 'teal']
    def skin_color_is_medium(self):
        return self.skincolor in ['tanned', 'brown', 'burnished']
    def die(self):
        self.alive = False
    def talk(self):
        if self:
            print "I don't want to go in the cart!"
    def walk(self, where=''):
        if self:
            if not where:
                print "Just taking a stroll..."
            else:
                print "On my way to " + where

--> from human import Human
--> charles = Human('blonde', 'teal', 'sunburnt')
--> charles
--> charles = Human('blonde', 'teal', 'sunburnt')
--> if charles:
...   charles.eye_color_is_green()
...   charles.walk("away from the cart...")
...   charles.talk()
...
True
On my way to away from the cart...
I don't want to go in the cart!
--------------------------------------------------------------------

If charles is alive is rather important to the whole object -- if he's dead, he's not walkin' and talkin'! ;)

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

Reply via email to