meta None?

Tripp Lilley tlilley at perspex.com
Mon Aug 21 05:02:25 EDT 2000


On Mon, 21 Aug 2000, Thomas Wouters wrote:

> On Mon, Aug 21, 2000 at 12:57:01AM -0400, Tripp Lilley wrote:
> 
> > I'd like to implement a meta-None class... that is, a class that behaves
> > like None regardless of what you subject it to:
> 
> *why* ?

Well, originally, I was going to say "I forgot", it being 0500 here :) But
I got a glass of water, thought for a minute, and now I remember:

My ultimate goal is to provide a "clean" integration between the prototype
world and the "normal" python world. Thus, when I pass an object cross the
membrane into a python programmer's hands, I want to behave as much like
whatever he/she expects it to as possible.

So far, with my wrapped primitives, I have a set of objects which look
like either values or methods depending on how you use them (through
flagrant use of __call__, __repr__, __str__, etc.).

	o.value		## returns a wrapper object
	o.value( )	## returns the internal "value" of the slot
	print o.value	## displays the string of the internal "value"
	repr(o.value)	## returns a repr suitable for rebuilding the wrapper


The problem is this:

	if o.value is None:
		print "value was empty!"


I can't do it, unless I've missed somethin'. Well, let me re-characterize
that. I can't do it in the context of my chameleonic wrappers, as above.
Let's say that o.value carries the "value" None. That is, ultimately, the
slot is "empty", but I don't want to throw an exception. I want to produce
an SQL "null" analog. I also want it to fit the paradigm I've already
established, which is that the results of a slot evaluation can operate in
just about any context you bend them to.

Now, if my slot is carrying a None in its belly, I'd want this:

	o.value			## None!
	o.value( )		## None!
	print o.value		## None! :)
	repr(o.value)		## None (or a wrapper, undecided)


If I put None in the internal "value" member, then this is what I get:

	o.value			## a _slot_readwrite object containing None
	o.value( )		## None
	print o.value		## None
	repr(o.value)		## None

which means that the first dereference won't be terribly useful for things
like "if o.value is None:" or what have you.

So, instead, I decide to return a literal 'None' in o's __getattr__,
bypassing the wrappers I usually use for primitives. Now I get:

	o.value			## None!
	o.value( )		## TypeError: call of non-function (type None)
	print o.value		## None
	repr(o.value)		## None

Which gives me, again, three out of four :) So my choice is either to
break the "is" test or to break the method-style invocation...


> You cannot. 'is' is not overloadable because it's the identity check: it
> determines whether the left and right operand refer to the *same object*.
> not something with a similar value. So, "don't do that then" is the answer.
> Either use '==', or work around the problem entirely differently. If you
> told us what you're trying to do, we might be able to help ;)

I guess it's not so much that *I* care to use is... I mean, the test:

	if o.value == None:

works just as well, right? And has the benefit of triggering all of my
wrappers' black magic so they appear to be "None" when they're supposed to
be. The question came up, though, when I came across docs and some code
that tests for None-ity with the "is" operator.

So perhaps the more important question is "is 'is' the right way to test
for None-ness?" If it's not the preferred style of testing for None-ness,
then my question is moot, and people who do 'is' can suffer :) On the
other hand, if 'is' *is* the preferred mechanism for testing against None,
then I have to make a decision :)

-- 
   Tripp Lilley  *  tripp at perspex.com  *  http://stargate.sg505.net/~tlilley/
------------------------------------------------------------------------------
   "Viral marketing is not going to save mediocre businesses from extinction.
   It is the scourge of the stupid and the slow, because it only rewards
   companies that offer great service and have the strength to allow and
   even encourage their customers to publicly pass judgment on that service
   every single day."

   - Clay Shirky, Business 2.0, "The Toughest Virus of All"





More information about the Python-list mailing list