Questions tagged [self]

A keyword used in instance methods to refer to the object on which they are working.

In many object-oriented programming languages, self (also called this or Me) is a keyword that is used in instance methods to refer to the object on which they are working. Languages like and others such as , and use self. uses self or super; and languages which derive in style from it (such as , , and ) generally use this. Visual Basic uses Me.

Invoking a method on the self searches for the method implementation of the method in the usual manner, starting in the dispatch table of the receiving object’s class.

Example:

[self startThread];
self.hostReach = YES;
BOOL value = self.hostReach;

Here, self is a variable name that can be used in any number of ways, even assigned a new value.

Inside an instance method, self refers to the instance; inside a class method, self refers to the class object.

1538 questions
1313
votes
26 answers

What is the purpose of the `self` parameter? Why is it needed?

Consider this example: class MyClass: def func(self, name): self.name = name I know that self refers to the specific instance of MyClass. But why must func explicitly include self as a parameter? Why do we need to use self in the…
richzilla
  • 40,440
  • 14
  • 56
  • 86
966
votes
18 answers

What do __init__ and self do in Python?

I'm learning the Python programming language and I've came across something I don't fully understand. In a method like: def method(self, blah): def __init__(?): .... .... What does self do? What is it meant to be? Is it…
GUIDED BOMB
494
votes
11 answers

"TypeError: method() takes 1 positional argument but 2 were given" but I only passed one

If I have a class ... class MyClass: def method(arg): print(arg) ... which I use to create an object ... my_object = MyClass() ... on which I call method("foo") like so ... >>> my_object.method("foo") Traceback (most recent call…
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
431
votes
9 answers

Why do I get "TypeError: Missing 1 required positional argument: 'self'" trying to call a method from the class directly?

I have some code like: class Pump: def __init__(self): print("init") def getPumps(self): pass p = Pump.getPumps() print(p) But I get an error like: Traceback (most recent call last): File…
DominicM
  • 6,520
  • 13
  • 39
  • 60
417
votes
7 answers

Difference between 'cls' and 'self' in Python classes?

Why is cls sometimes used instead of self as an argument in Python classes? For example: class Person: def __init__(self, firstname, lastname): self.firstname = firstname self.lastname = lastname @classmethod def…
Scaraffe
  • 5,041
  • 5
  • 21
  • 20
220
votes
10 answers

Why do you need explicitly have the "self" argument in a Python method?

When defining a method on a class in Python, it looks something like this: class MyClass(object): def __init__(self, x, y): self.x = x self.y = y But in some other languages, such as C#, you have a reference to the object that…
readonly
  • 343,444
  • 107
  • 203
  • 205
214
votes
13 answers

Python decorators in classes

Can one write something like: class Test(object): def _decorator(self, foo): foo() @self._decorator def bar(self): pass This fails: self in @self is unknown I also tried: @Test._decorator(self) which also fails: Test…
hcvst
  • 2,665
  • 2
  • 22
  • 25
195
votes
7 answers

Instance variable: self vs @

Here is some code: class Person def initialize(age) @age = age end def age @age end def age_difference_with(other_person) (self.age - other_person.age).abs end protected :age end What I want to know is the difference…
sarunw
  • 8,036
  • 11
  • 48
  • 84
157
votes
11 answers

How to avoid explicit 'self' in Python?

I have been learning Python by following some pygame tutorials. Therein I found extensive use of the keyword self, and coming from a primarily Java background, I find that I keep forgetting to type self. For example, instead of self.rect.centerx I…
bguiz
  • 27,371
  • 47
  • 154
  • 243
125
votes
5 answers

What does new self(); mean in PHP?

I've never seen code like this: public static function getInstance() { if ( ! isset(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; } Is it the same as new className() ? EDIT If the class is…
user198729
  • 61,774
  • 108
  • 250
  • 348
99
votes
2 answers

WPF Bind to itself

I've got a WPF Window, and somewhere there is a ListView where I bind a List to. Now somewhere in my ListView there is a TextBox and the Content property is set to {Binding}. But this is the shorthand. How do I write the full binding to…
Anemoia
  • 7,928
  • 7
  • 46
  • 71
88
votes
4 answers

Difference between Python self and Java this

I had done a bit of Python long back. I am however moving over to Java now. I wanted to know if there were any differences between the Python "self" method and Java "this". I know that "self" is not a keyword while "this" is. And that is pretty much…
Bayko
  • 1,344
  • 2
  • 18
  • 25
84
votes
4 answers

When to use self in Model?

Question: when do I need to use self in my models in Rails? I have a set method in one of my models. class SomeData < ActiveRecord::Base def set_active_flag(val) self.active_flag = val self.save! end end When I do this, everything works…
varatis
  • 14,494
  • 23
  • 71
  • 114
82
votes
2 answers

Python - self, no self and cls

Yet another question on what the 'self' is for, what happens if you don't use 'self' and what's 'cls' for. I "have done my homework", I just want to make sure I got it all. self - To access an attribute of an object, you need to prefix the attribute…
Joelmc
  • 873
  • 2
  • 8
  • 5
81
votes
7 answers

Distinction in Swift between uppercase "Self" and lowercase "self"

While playing in a Swift playground I noticed that Self, with capital "S", is available along with the lowercase self. Is there any difference between them? If so, what are usages for these two, especially for Self?
Eray Diler
  • 1,095
  • 1
  • 10
  • 17
1
2 3
99 100