How to make a custom type "true" or not, in Python 1.5.2

Warren Postma embed at geocities.com
Mon Feb 19 16:36:28 EST 2001


"Michael Hudson" <mwh21 at cam.ac.uk> wrote in message
news:m3r90utl89.fsf at atrus.jesus.cam.ac.uk...
> "Warren Postma" <embed at geocities.com> writes:
>
> You want nb_nonzero in the number methods struct, from memory...  and
> the nb_nonzero field from the tp_as_number field from checking the
> source (! where does my brain keep all these bits of information?).
>

Alas it appears you can't just fill this in. I looked at MxDateTime as a
sample extension, and it says the first 10 items in the tp_as_number table
must be non-null or python will crash, thus you have to write a little stub
function. Kind of yucky. My objects have no possible numeric protocol.

So I tried my earlier hunch about if len(x) is 0, even if the rest of the
sequence protocol is undefined, it seems to work.  The weird thing here is
that len(x) doesn't mean anything, other than I suppose, the object is an
empty set if len(x) is zero.

That seemed to work.  Has anyone got any opinion on the Relative Goodness or
Badness of using Len(x) => 0 to impute Non-Thingy-ness upon some thing, when
implementing the whole tp_as_number protocol is not the Right Thing to Do?

;-)

Warren






More information about the Python-list mailing list