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

Ruby Definition of Self

I was reading a Ruby book and came across this definition of the pseudo-variable self: self - receiver object of the current method Could someone break down that definition and explain what it means? I don't understand any of it. EDIT: I…
Devoted
  • 177,705
  • 43
  • 90
  • 110
13
votes
4 answers

How does a python method automatically receive 'self' as the first argument?

Consider this example of a strategy pattern in Python (adapted from the example here). In this case the alternate strategy is a function. class StrategyExample(object): def __init__(self, strategy=None) : if strategy: …
Praveen Gollakota
  • 37,112
  • 11
  • 62
  • 61
13
votes
1 answer

Using self in Django Model classes

While adding model class to models.py in Django, why don't we use self with the field variables which we define? Shouldn't not using self field variables make them class variables instead,which "may" cause a problem.
Kartik Rustagi
  • 669
  • 2
  • 8
  • 16
13
votes
2 answers

Passing self into a constructor in python

I recently was working on a little python project and came to a situation where I wanted to pass self into the constructor of another object. I'm not sure why, but I had to look up whether this was legal in python. I've done this many times in C++…
Falmarri
  • 47,727
  • 41
  • 151
  • 191
13
votes
3 answers

Difference between yield self and yield?

Could anyone please help me to understand the difference between "yield self" and "yield"? class YieldFirstLast attr_accessor :first, :last def initialize(first = nil, last = nil) @first = first @last = last yield…
vivek kumar
  • 179
  • 3
  • 8
12
votes
1 answer

Lua: colon notation, 'self' and function definition vs. call

I'm getting terribly confused by the colon notation used when defining/calling Lua functions. I thought I'd got my head round it until I saw this piece of code: function string.PatternSafe( str ) return ( str:gsub( ".",…
cabbageforall
  • 620
  • 1
  • 5
  • 12
12
votes
3 answers

class, dict, self, init, args?

class attrdict(dict): def __init__(self, *args, **kwargs): dict.__init__(self, *args, **kwargs) self.__dict__ = self a = attrdict(x=1, y=2) print a.x, a.y b = attrdict() b.x, b.y = 1, 2 print b.x, b.y Could somebody explain…
kame
  • 20,848
  • 33
  • 104
  • 159
11
votes
2 answers

Inheriting instance variables in Objective-c

In Objective-c 2.0 why do subclasses need to reference instance variables in parent classes using the self keyword? Consider this example: // a.h @interface MyClass : NSObject @property (nonatomic, retain) Object *myObject; @end //…
SundayMonday
  • 19,147
  • 29
  • 100
  • 154
11
votes
3 answers

Ruby's self vs. Python's self

Possible Duplicate: What is the difference between Ruby and Python versions of“self”? Ruby and Python are similar languages that both have a self keyword used in various situations. What does self mean in each language, and what are the…
jrdioko
  • 32,230
  • 28
  • 81
  • 120
11
votes
1 answer

How to reference `self` in dataclass' fields?

I am trying to do the equivalent of: class A: def __init__(self): self.b = self.get_b() def get_b(self): return 1 using @dataclass. I want to use a @dataclass here because there are other (omitted) fields initialized from…
Mario Ishac
  • 5,060
  • 3
  • 21
  • 52
11
votes
4 answers

Java "self" (static) reference

I am looking for a "self" reference to the current class in JAVA in a static context manner like in PHP Scope Resolution Operator? Solution: Break out of scope? BEWARE, this is compared to a static definition really slow (by factor 300): static…
feffi
  • 113
  • 1
  • 1
  • 5
11
votes
4 answers

Delphi self keyword

I am learning Delphi reading Marco Cantu's book and it's super complete. It's very clear but I have a doubt about the keyword self. I already have experience with OOP and I have the basics of it. My question is very simple. Can I compare the keyword…
Alberto Miola
  • 4,643
  • 8
  • 35
  • 49
11
votes
5 answers

How to get self into a Python method without explicitly accepting it

I'm developing a documentation testing framework -- basically unit tests for PDFs. Tests are (decorated) methods of instances of classes defined by the framework, and these are located and instantiated at runtime and the methods are invoked to…
kindall
  • 178,883
  • 35
  • 278
  • 309
10
votes
4 answers

Attribute assignment to built-in object

This works: class MyClass(object): pass someinstance = MyClass() someinstance.myattribute = 42 print someinstance.myattribute >>> 42 But this doesn't: someinstance = object() someinstance.myattribute = 42 >>> AttributeError: 'object' object…
Turion
  • 5,684
  • 4
  • 26
  • 42
10
votes
1 answer

What does the 'self' keyword mean in WebWorkers

I don't understand the 7,8,9 line: var worker = new Worker('doWork.js'); worker.addEventListener('message', function(e) { console.log('Worker said: ', e.data); // Here it send's the data. }, false); worker.postMessage('Hello World'); // Send…