c[:]()

Erik Max Francis max at alcyone.com
Fri Jun 1 01:53:05 CEST 2007


Warren Stringer wrote:

> I'm still a bit new at this, was wondering why c[:]() doesn't work, and
> implicitly wondering why it *shouldn't* work. 

It does work.  It means "make a sliced copy of `c`, and then call it 
with no arguments."  Functionally that is _no different_ from `c()`, 
which means "take `c` and call it with no arguments," because presuming 
`c` is a list, `c[:]` makes a shallow copy.

So if you think `c()` and `c[:]()` should do something different in this 
case, you are profoundly confused about Python's semantics of what 
objects are, what it means to shallow copy a list, and what it means to 
make a function call.  That you keep including the slice suggests that 
there's something about its meaning that's not yet clicking.

If you really want syntax where a function call on a container calls all 
of its elements, then that is trivially easy to do by creating such an 
object and overriding its `__call__` method.

If you're not willing to do that, but still insisting that `c[:]()` 
makes sense, then perhaps it would be more advisable to learn more about 
Python rather than try to suggest profound changes to the language and 
its conventions.

-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
  San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
   The quality, not the longevity, of one's life is what is important.
    -- Dr. Martin Luther King, Jr.



More information about the Python-list mailing list