Asterisk sign before the 'self' keyword

Robert Kern robert.kern at gmail.com
Fri Feb 11 11:10:03 EST 2011


On 2/11/11 9:06 AM, christian.posta wrote:
> I searched quickly to see whether this may have been discussed before,
> but it's possible my search criteria was not refined enough to get any
> hits. Forgive me if this is a silly question..
>
> I was reading some Python code from a third-party app for the django
> project... i saw this in the code and I wasn't certain what it means,
> nor could I find anything helpful from google.
>
> Within the __call__ function for a class, I saw a method of that class
> referred to like this:
>
> *self.<method_name_here>()
>
> The brackets indicate the method name.
> What does the *self refer to??
> Does it somehow indicate the scope of the 'self' variable?

Can you show the whole statement? Most likely, this was embedded in some other 
call, e.g.:

   foo(*self.method())

If this is the case, the * does not bind to "self"; it binds to all of 
(self.method()), i.e.:

   foo(*(self.method()))

This is just the foo(*args) syntax that unpacks a tuple into individual 
arguments to pass to foo().

http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list