Storing empties

Aahz aahz at pythoncraft.com
Tue Nov 8 17:34:55 EST 2005


In article <1h5d3k0.32mnkz17x2xh6N%aleax at mail.comcast.net>,
Alex Martelli <aleax at mail.comcast.net> wrote:
>Aahz <aahz at pythoncraft.com> wrote:
>>Alex:
>>>Aahz:
>>>>Alex:
>>>>>
>>>>>the canonical idiom when you need such distinction is:
>>>>>
>>>>>_not_there = object()
>>>>
>>>> What's your preferred idiom when you're dealing with storable objects?
>>>
>>>What's a "storable object"?  You mean, something that can be pickled, or
>>>passed to the .write method of a file object, or stored in a database,
>>>or what else?
>> 
>> Pickled and/or stored in a DB.
>
>Relational databases have the concept of NULL specifically to indicate
>"there is NOTHING here".  I love it.

Right.

>For pickling, object() as a unique "nothing here, NOT EVEN a None"
>marker (AKA sentinel) works fine.

How does that work?  Maybe I'm missing something obvious.

    sentinel = object()
    class C:
        def __init__(self, foo=sentinel):
            self.foo = foo
        def process(self):
            if self.foo is not sentinel:
                ....

Now, the way I understand this, when your application restarts and an
instance of C is read from a pickle, your sentinel is going to be a
different instance of object() and process() will no longer work
correctly.  Are you suggesting that you need to pickle the sentinel with
the instance?  Or is there some other trick I'm missing?
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"If you think it's expensive to hire a professional to do the job, wait
until you hire an amateur."  --Red Adair



More information about the Python-list mailing list