You are not logged in.

#1 2008-10-07 17:51:24

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

[Python] What is self used for? I just can't get it...

Please, give me an example. I'm tired of definitions and such that I find in the web. I can't learn these damn classes... sad

Offline

#2 2008-10-07 18:03:33

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: [Python] What is self used for? I just can't get it...

class Pen(object):

    def __init__(self, colour, type):
        self.colour = colour
        self.type = type

    def get_colour(self):
        return self.colour

    def get_type(self):
        return self.type

    def print_info(self):
        print "colour: %s" % self.get_colour()
        print "type: %s" % self.get_type()

In use:

>>> p = Pen('red', 'felt tipped')
>>> p.colour
'red'
>>> p.get_colour()
'red'
>>> p.print_info()
colour: red
type: felt tipped
>>> 
>>> Pen.get_colour(p)
'red'

Basically, 'self' is a way to reference the object you are currently 'inside'. So if you had created an object and called it 'p', you could access its attribute with p.colour. But if you're inside that object, how do you access the attribute? that's where self comes in.

when you call p.get_colour(), python actually calls Pen.get_colour(), but automatically passes p as the first argument, which goes into self. That's why:

p.get_colour()
and
Pen.get_colour(p)

are more or less identical.

Hope this helps!

Dusty

Offline

#3 2008-10-07 18:04:29

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: [Python] What is self used for? I just can't get it...

Suppose you are making a class that describes cars and the car has his type of fuel as a class variable, Like this: (java syntax, I don't know python that well, but you'll get the idea)

public class car {
  private String fueltype;
}

You can now refer to that variable as fueltype or as this.fueltype ("this" is what is called "self" in python). At first sight this gives no more power, but consider this method:

public class car{
  private String fueltype;

  public void setFueltype(String fueltype) {
    if ( ... ) {
      this.fueltype = fueltype;
    }
  }
}

See the difference? fueltype refers to the variable with the smallest scope, here that is the local variable passed as an argument. If you want to refer to the instance variable, you use "this" (self), which means "this object", so you ask after this objects property called "fueltype".

Last edited by Ramses de Norre (2008-10-08 10:35:15)

Offline

#4 2008-10-07 18:20:51

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Re: [Python] What is self used for? I just can't get it...

Thanks, Dusty, I think I get it... sort of..

Ramses de Norre, when I start learning Java I'll get here and read it again. smile Thanks anyway.

Offline

#5 2008-10-07 19:37:16

Husio
Member
From: Europe
Registered: 2005-12-04
Posts: 359
Website

Re: [Python] What is self used for? I just can't get it...

Example class and function:

class A(object):
    def __init__(self):
        self.x = 1

    def set_x(self, value):
        self.x = value

    def set_x_2(object, v):
        object.x = v

    def do_not_set_x(self, value):
        x = value


def fun_set_x(object, v):
    object.x = v

and ipython session:

In [2]: a = A()

In [3]: a.x
Out[3]: 1

In [4]: a.set_x(2)

In [5]: a.x
Out[5]: 2

In [6]: a.set_x_2(3)

In [7]: a.x
Out[7]: 3

In [8]: a.do_not_set_x(4)

In [9]: a.x
Out[9]: 3

In [10]: fun_set_x(a, 5)

In [11]: a.x
Out[11]: 5

Offline

#6 2008-10-07 20:30:49

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Re: [Python] What is self used for? I just can't get it...

Thanks, Husio. smile

Offline

#7 2008-10-07 21:07:39

freakcode
Member
From: São Paulo - Brazil
Registered: 2007-11-03
Posts: 410
Website

Re: [Python] What is self used for? I just can't get it...

Note: the name 'self' is just a tradition. You can call it anyway you like, as long it is the first in the argument list, will always receive a reference to the current object. So, in constrast, 'self' is not a reserved word like 'this' in other languages - it's explicity declared.

Offline

#8 2008-10-08 10:34:24

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: [Python] What is self used for? I just can't get it...

Boris Bolgradov wrote:

Thanks, Dusty, I think I get it... sort of..

Ramses de Norre, when I start learning Java I'll get here and read it again. smile Thanks anyway.

Don't bother the syntax, look at the difference between the referencing to variables. The situation is completely identical in python and java.

Offline

#9 2008-10-08 13:06:15

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: [Python] What is self used for? I just can't get it...

Not quite completely identical -- self is an explicit reference in python. Some have even said its not truly object oriented because of that.

Dusty

Offline

#10 2008-10-08 13:23:41

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: [Python] What is self used for? I just can't get it...

True, but the use is the same.

Offline

Board footer

Powered by FluxBB