another puzzled newbie

Steven Taschuk staschuk at telusplanet.net
Fri Aug 29 09:51:09 EDT 2003


Quoth Elaine Jackson:
> "Chad Netzer" <cnetzer at sonic.net> wrote in message
> news:mailman.1062051053.30902.python-list at python.org...
  [...]
> | You don't need the '(' and ')' here.  del is a keyword, not a function.
> 
> Can't it be both? Could you explain why it fails to be a function?

Since 'del' is a keyword, it cannot be used as a variable name:

    >>> del = 3
      File "<stdin>", line 1
        del = 3
            ^
    SyntaxError: invalid syntax

    >>> def del(x): return 11*x
      File "<stdin>", line 1
        def del(x): return 11*x
              ^
    SyntaxError: invalid syntax

The token 'del' has special significance in Python syntax; it can
only introduce a del statement.

It is mostly harmless to put parentheses around what follows
'del', but it can create the misleading impression that you are or
think you are calling a function named 'del'.  Besides Erik's
point that there's no return value, note also that the "argument"
to del is not an argument at all -- it's not an expression which
is evaluated to provide a value to del.  For example, after
    def foo(x):
        return 11*x
    num = 3
the calls
    foo(num)
and
    foo(3)
are equivalent -- the function foo cannot tell the difference.
The expression 'num' is evaluated to produce the value 3, and then
that value is passed to the function.  In contrast,
    del num
    del 3
are not equivalent, even when num == 3; the former deletes the
name 'num' (regardless of its value), while the latter is
syntactically erroneous.

What follows 'del' is actually a target (list), just like the
left-hand side of an assignment statement, and not an argument
(list).

  [...]
> No, I want (-1) the integer. My problem is that I really dislike the
> appearance
> of a statement like X-=Y. (Even right now I can barely stand writing it.)
> So I
> wrote X+=(-Y) instead. Maybe it would look better as
> shuffler=map(lambda n: n-1, shuffler)?

I'd prefer
    shuffler[:] = [x-1 for x in shuffler]
but this is mostly a matter of taste.  (Note the use of [:] to
modify the existing list in place, as the context requires.)

-- 
Steven Taschuk              Aral: "Confusion to the enemy, boy."
staschuk at telusplanet.net    Mark: "Turn-about is fair play, sir."
                             -- _Mirror Dance_, Lois McMaster Bujold





More information about the Python-list mailing list