dummy/trash object

Guido van Rossum guido at python.org
Mon Jul 9 09:57:46 EDT 2001


Xavier Defrang <xavier at perceval.net> writes:

> Hello,
> 
> I'm sure that someone already came with a question like : "why is there no
> real dummy or trash object in Python which could by used like the magic
> '_' in Prolog?"
> 
> Using a "normal" variable like in the following snippet involves useless
> assignations...
> 
> <snip>
> >>> t = 1, 2, 3, 4
> >>> x, _, z, _ = t
> >>> _
> 4
> </snip>
> 
> Since I'm quite sure that the language designers have already thought
> about this, the question is not whether such a magic object should be
> added to the Python language but why it has not been done?  I'm sure there
> are good reasons... The nearly-magic None value could have been used for
> this purpose... Some could say that it would break the "no exception" rule
> but we've already seen in previous threads (about assigning a value to
> None) that the core of the interpreter has some special instructions to
> handle None thus turning it into an actual exceptional thing...

Assignments are about the fastest operations in Python, as there is no
data copying involved -- only a pointer copy.  So you don't have to
worry about the "cost" of the assignment.  Adding a special case would
probably slow things down.  If you're worried about keeping the unused
object alive longer than necessary (which you normally shouldn't worry
about unless it's many megabytes is size), assign something else to
the same name -- this will delete the object previously held in that
name, *if* it was the last and only reference.  Or you can use the
'del' statement.

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-list mailing list