Use cases for del

Ron Adam rrr at ronadam.com
Thu Jul 7 13:57:55 EDT 2005


Grant Edwards wrote:

> On 2005-07-07, Ron Adam <rrr at ronadam.com> wrote:

>>>>>>It would be a way to set an argument as being optional without
>>>>>>actually assigning a value to it.
>>
>>So it would still work like you expect even though v is not
>>bound to anything.  Like I said the bigger problem is that
>>globals will be visible and that would create a conflict.
>>Setting a value to None in a function hides globals of the
>>same name.  That wouldn't happen if None unbound names as del
>>does.
> 
> Good point.  I hadn't thought of that.

The easiest way to fix that is to create a Nil or Null object as a 
replacement for the current None object.


>>>I find it more obvious to set the name to None during the
>>>periods that it isn't valid than to delete it and check for a
>>>NameError when I want to know if the value is usable or not.
>>
>>You would still be able to do that explicitly and it probably would be a 
>>good idea when you aren't sure if a name is left over from something else.
>>
>>If a name is None, it means it's available and unassigned, so
>>you don't have to check for a NameError.
> 
> 
> How would you spell "if a name is None"?

How about:

if name is None: name = something

Making None the same as undefined wouldn't change this.  I think the 
parser would need to be a little smarter where None is concerned in 
order to be able to handle it in bool expressions,  and catch improper 
name = undefined assignments.

But it might not change things as much as you think.


> Personally, I think the spellings
> 
>    del name
>    if 'name' in locals()
> 
> is much more explicit/obvious than
> 
>    name = None
>    name is None   

This would be
      name = None                     # remove it from locals()
      if name is None: dosomething    # if it's not in locals()

No need to check locals or globals.  That's one of the plus's I think.


Since we can already assign a value to any name without first 
initializing it,  this just allows us to use it in a some expressions 
without also first initializing it.  It would still give an error if we 
tried to use it in "any" operation.  Just like the present None.


> I expect the "=" operator to bind a name to a value.  Having it
> do something completely different some of the time seems a bit
> unpythonic.

But you are assigning it a value. Instead of having a Name assigned to a 
None object, the name is just removed from name space to do the same 
thing and you save some memory in the process.



How about keeping None as it is as a place holder object, and having a 
keyword called "undefined" instead.

x = 3
x = undefined   # delete x from name space
if x is not undefined: print x

if an undefined name is used with any operator, it could give a 
'UndefinedName' error.

This would be more compatible with the current python language.

Cheers,
Ron




More information about the Python-list mailing list