Why the 'self' argument?

vivek at cs.unipune.ernet.in vivek at cs.unipune.ernet.in
Fri Sep 5 10:22:36 EDT 2003


On Fri, Sep 05, 2003 at 01:58:41PM +0000, Grzegorz Staniak wrote:
> Hello,
> 
> I'm a newbie Python user, a systems administrator - I've been trying
> to switch from Perl to Python for administrative tasks - and one thing 
> I cannot understand so far is why I need the special 'self' (or anything
> esle) argument in class method definitions. I might have missed an
> explanation in the docs, a quick Google search did not help. Is there
> somewhere on the web you could direct me to?
> 
> Thanks,
> 
> -- 
> Grzegorz Staniak <gstaniak at zagiel.com.pl>
> -- 
> http://mail.python.org/mailman/listinfo/python-list

The first argument in a class method is a reference to that class itself. It is
same as "this" pointer in c++ and java. Using this argument you can access the 
class variables. ex:

class test:
  def __init__(self,arg1,arg2): #this acts as class constructor
    self.name=arg1
    self.value=arg2


  def changeName(self,newName): #here we change the name class variable
    self.name=newName
  ..........and so on

regards
Vivek Kumar





More information about the Python-list mailing list