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
34
votes
5 answers

What does "self" mean in javascript?

I read here that "self Refers to the current window or form". Self does not seem to refer to the current form in this case:
However in this case it works (referring to…
Nick Van Brunt
  • 15,244
  • 11
  • 66
  • 92
34
votes
3 answers

PHPUnit - Use $this or self for static methods?

I don't want to write a long text, because it is a short question. PHPUnit tests contain several methods that are static. For example all those \PHPUnit\Framework\Assert::assert*() methods and also the identicalTo, equalTo. My IDE (with…
Wolfsblvt
  • 1,061
  • 12
  • 27
32
votes
5 answers

Swift Error: Ambiguous reference to member 'subscript'

I'm new to coding and picked up some open source project to get the idea. I'm getting the error: Ambiguous reference to member 'subscript' in the code below: let pictures = ( selectedRestaurant["Pictures"] as! NSArray ) // Error let picture = (…
jonasdickel
  • 321
  • 1
  • 3
  • 4
31
votes
3 answers

Can anybody please explain (my $self = shift) in Perl

I'm having a really hard time understanding the intersection of OO Perl and my $self = shift; The documentation on these individual elements is great, but none of them that I've found touch on how they work together. I've been using Moose to make…
Alex H Hadik
  • 774
  • 2
  • 7
  • 16
31
votes
8 answers

How to keep track of class instances?

Toward the end of a program I'm looking to load a specific variable from all the instances of a class into a dictionary. For example: class Foo(): def __init__(self): self.x = {} foo1 = Foo() foo2 = Foo() ... Let's say the number of…
DBWeinstein
  • 8,605
  • 31
  • 73
  • 118
30
votes
6 answers

Can a JavaScript function return itself?

Can I write a function that returns iteself? I was reading some description on closures - see Example 6 - where a function was returning a function, so you could call func()(); as valid JavaScript. So I was wondering could a function return itself…
fbas
  • 1,676
  • 3
  • 16
  • 26
30
votes
2 answers

need self to set all constants of a swift class in init

I have a Swift class that has a constant ivar (are they called instance constants now?). To set the value to this constant, I need to call an initializer of the desired object and pass itself. However, I am not allowed to as I need to initialize all…
Michael Ochs
  • 2,846
  • 3
  • 27
  • 33
29
votes
8 answers

How to create routes with FastAPI within a class

So I need to have some routes inside a class, but the route methods need to have the self attr (to access the class' attributes). However, FastAPI then assumes self is its own required argument and puts it in as a query param This is what I've…
Nexy7574
  • 514
  • 1
  • 5
  • 10
29
votes
2 answers

PHP: self:: vs parent:: with extends

I'm wondering what is the difference between using self:: and parent:: when a static child class is extending static parent class e.g. class Parent { public static function foo() { echo 'foo'; } } class Child extends Parent { …
djkprojects
  • 425
  • 1
  • 5
  • 8
29
votes
6 answers

Assigning to self in Objective-C

I'm from the C++ world so the notion of assigning this makes me shudder: this = new Object; // Gah! But in Objective-C there is a similar keyword, self, for which this is perfectly acceptable: self = [super init]; // wait, what? A lot of sample…
fbrereto
  • 35,429
  • 19
  • 126
  • 178
28
votes
2 answers

(Ruby,Rails) Context of SELF in modules and libraries...?

Quick question regarding the use of "SELF" inside a module or library. Basically what is the scope/context of "SELF" as it pertains to a module or library and how is it to be properly used? For an example of what I'm talking about, check out the…
humble_coder
  • 2,777
  • 7
  • 34
  • 46
28
votes
2 answers

Swift - perform Segue

if (view.annotation.title as String!) == "Helgoland " { currentLong = 7.889021 currentLat = 54.180210 url = "google.com" let alertController: UIAlertController = UIAlertController(title: "Change Map Type", message: nil, preferredStyle:…
Fabian Boulegue
  • 6,476
  • 14
  • 47
  • 72
24
votes
4 answers

What does 'self' refer to in a @classmethod?

I thought I was starting to get a grip on "the Python way" of programming. Methods of a class accept self as the first parameter to refer to the instance of the class whose context the method is being called in. The @classmethod decorator refers to…
Yes - that Jake.
  • 16,725
  • 14
  • 70
  • 96
24
votes
4 answers

Why does Array#each return an array with the same elements?

I'm learning the details of how each works in ruby, and I tried out the following line of code: p [1,2,3,4,5].each { |element| el } And the result is an array of [1,2,3,4,5] Why is the return value of each the same array? Doesn't each just provide…
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
23
votes
5 answers

python global name 'self' is not defined

Just started learning python and I am sure its a stupid question but I am trying something like this: def setavalue(self): self.myname = "harry" def printaname(): print "Name", self.myname def main(): …
harry
  • 239
  • 1
  • 2
  • 3