Asterisk sign before the 'self' keyword

Ian Kelly ian.g.kelly at gmail.com
Fri Feb 11 11:02:54 EST 2011


On 2/11/2011 8: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?
> Thanks in advance...

If the line appeared exactly as you have it here, then it would be a 
syntax error.  More likely is that you saw something like this:

self.do_something(*self.return_a_sequence())

Here, "return_a_sequence" could return any sequence or iterable.  The * 
indicates that whatever follows it is to be unpacked and passed into 
"do_something" as a series of individual arguments.  For example, if 
"return_a_sequence" returns the tuple (1, 2, 3), then the above would be 
functionally equivalent to:

self.do_somethign(1, 2, 3)




More information about the Python-list mailing list