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
23
votes
3 answers

how to use a Python function with keyword "self" in arguments

i have a function that retrieve a list of stores in Python this functions is called : class LeclercScraper(BaseScraper): """ This class allows scraping of Leclerc Drive website. It is the entry point for dataretrieval. """ def…
imoum
  • 405
  • 3
  • 6
  • 13
22
votes
3 answers

Does @synchronized(self) create a block where the self prefix is unecessary on properties?

I have read something in some foreign code and I want to check my assumption: @synchronized(self) is used to get rid of the self prefix when setting a property. So in my example below, I'm setting the strText of the instance, not just a local…
endo.anaconda
  • 2,449
  • 4
  • 29
  • 55
19
votes
1 answer

When trying to invoke def, I get: parameter 'self' unfilled

I'm trying to write a class to randomly pick a number of songs from a dictionary. The whole point of it is to be able to simply type DJ.chooseListing() for example to execute the random selection. Alas it gives me an error I can't really find…
Thom Ernst
  • 379
  • 1
  • 4
  • 15
18
votes
2 answers

What's the Point of Using [self class]

Is this code correct @implementation Vehicle +(id) vehicleWithColor:(NSColor*)color { id newInstance = [[[self class] alloc] init]; // PERFECT, the class is // dynamically identified [newInstance setColor:color]; return [newInstance…
user4951
  • 32,206
  • 53
  • 172
  • 282
18
votes
4 answers

In Ruby, inside a class method, is self the class or an instance?

I know that self is the instance inside of an instance method. So, then, is self the class inside of a class method? E.g., Will the following work in Rails? class Post < ActiveRecord::Base def self.cool_post self.find_by_name("cool") …
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
18
votes
3 answers

Why use [ClassName alloc] instead of [[self class] alloc]?

I'm reading through Mark Dalrymple's Learn Objective-C on the Mac (only at the chapter on Protocols, so still relatively newbish) and trying to figure something out: Why would you ever reference a class by its own name? If I had a class called Foo,…
Nick Sweet
  • 2,030
  • 3
  • 31
  • 48
18
votes
2 answers

Python self and super in multiple inheritance

In Raymond Hettinger's talk "Super considered super speak" at PyCon 2015 he explains the advantages of using super in Python in multiple inheritance context. This is one of the examples that Raymond used during his talk: class DoughFactory(object): …
Marc Tudurí
  • 1,869
  • 3
  • 18
  • 22
17
votes
5 answers

Why is Self assignable in Delphi?

This code in a GUI application compiles and runs: procedure TForm1.Button1Click(Sender: TObject); begin Self := TForm1.Create(Owner); end; (tested with Delphi 6 and 2009) why is Self writable and not read-only? in which situations could this be…
mjn
  • 36,362
  • 28
  • 176
  • 378
17
votes
3 answers

Rails -- self vs. @

I am following Michael Hartl's RoR tutorial, and it is covering the basics of password encryption. This is the User model as it currently stands: class User < ActiveRecord::Base attr_accessor :password attr_accessible :name, :email,:…
Kvass
  • 8,294
  • 12
  • 65
  • 108
17
votes
1 answer

How to use self parameter, @staticmethod keyword inside a class and its methods

I have a python class which has multiple methods. I have defined my methods via @staticmethod instance and I want to call other methods of my class from inside my main function(main_function). I think I need self parameter for calling my other…
Stateless
  • 293
  • 2
  • 4
  • 18
17
votes
4 answers

When to use self in module's methods

My module definition looks like this: module RG::Stats def self.sum(a, args = {}) a.inject(0){ |accum, i| accum + i } end end To use this method I simply require the file containing this definition so that I can…
Rojj
  • 1,170
  • 1
  • 12
  • 32
16
votes
3 answers

PHP - self, static or $this in callback function

Is it possible to access classes/objects reffered as self, static and $this in anonymous callbacks in PHP? Just like this: class Foo { const BAZ = 5; public static function bar() { echo self::BAZ; // it works OK …
Pavel S.
  • 11,892
  • 18
  • 75
  • 113
15
votes
3 answers

In Ruby, when should you use self. in your classes?

When do you use self.property_name in Ruby?
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
15
votes
1 answer

"self" object for UIViewcontroller has @"0 objects" in debug window in xcode

I segue to a Viewcontroller that immediately has 0 objects for "self" and although I can access "self." objects, I cannot see them in the debug window. Without pasting the code, is there a known reason for this to be happening? Thanks! to clarify: …
Tre K.
  • 151
  • 4
15
votes
2 answers

Difference between static:: and $this::

I know there is a difference between static:: and self:: like in this example ( from https://stackoverflow.com/a/13613718/2342518 )
Xenos
  • 3,351
  • 2
  • 27
  • 50