Asterisk sign before the 'self' keyword
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Fri Feb 11 17:49:03 EST 2011
On Fri, 11 Feb 2011 07:06:11 -0800, christian.posta wrote:
> 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...
self is not a keyworld. It is an ordinary variable name like any other,
except that by convention it is used to refer to the instance inside
methods.
Without knowing the context, *self.method() could mean at least one of
two things:
(1) Ordinary multiplication:
result = 42*self.method()
This is like:
temp = self.method()
result = 42*temp
(2) Positional argument unpacking:
result = function(*self.method())
This is like:
temp = self.method()
result = function(temp[0], temp[1], temp[2], ...)
or if you prefer, like the old and now deprecated function:
result = apply(function, temp)
--
Steven
More information about the Python-list
mailing list