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
10
votes
1 answer

Ruby: calling private methods from inside with self keyword

class MyClass def test puts my_id puts self.my_id end private def my_id 115 end end m = MyClass.new m.test This script results in an output: 115 priv.rb:4:in `test': private method `my_id' called for #
Paul
  • 25,812
  • 38
  • 124
  • 247
10
votes
1 answer

Aliasing this in scala with self =>

Some Scala APIs alias this to self, for example, trait Function1[-T1, +R] extends AnyRef { self => I know how this aliasing works in general, but don't see how traits such as Function1 benefit from it. Function1 does not use self anywhere in its…
user2325541
  • 101
  • 2
10
votes
3 answers

iOS: Usage of self and underscore(_) with variable

Possible Duplicate: How does an underscore in front of a variable in a cocoa objective-c class work? I have been very confused with using self or underscore with variable name after synthesizing it like below: In .h file: @property(nonatomic,…
Paresh Masani
  • 7,474
  • 12
  • 73
  • 139
9
votes
2 answers

How to change self in a block like instance_eval method do?

instance_eval method change self in its block, eg: class D; end d = D.new d.instance_eval do puts self # print something like #, not 'main'! end If we define a method ourself(or any other methods(other than instance_eval) which…
gaols
  • 341
  • 5
  • 13
9
votes
2 answers

self referencing update using MongoDB

I wonder if there is a way to make a self referencing update in MongoDB, so you can use object's params on a $set query. Here is an example: > db.labels.save({"name":"label1", "test":"hello"}) > db.labels.save({"name":"label2", "test":"hello"}) >…
fertapric
  • 103
  • 1
  • 4
9
votes
4 answers

self in python decorators

I want a decorator that would add the decorated function to list, like this : class My_Class(object): def __init__(self): self.list=[] @decorator def my_function(self) print 'Hi' I expect my_function to be added to…
CGGJE
  • 465
  • 4
  • 7
9
votes
3 answers

What does self do?

Possible Duplicate: Python 'self' keyword Forgive me if this is an incredibly noobish question, but I never did understand self in Python. What does it do? And when I see things like def example(self, args): return self.something what do…
user775171
9
votes
6 answers

When should I use the “self” keyword?

When should I be using the self expression in my iphone development applications? say i have 2 fields: UITextField *text1; and NSString *str1; retained and synthesized. when i am accessing either of these 2 fields, when should i and when should i…
james
  • 26,141
  • 19
  • 95
  • 113
9
votes
4 answers

Explicit passing of Self when calling super class's __init__ in python

This question is in relation to posts at What does 'super' do in Python? , How do I initialize the base (super) class? , and Python: How do I make a subclass from a superclass? which describes two ways to initialize a SuperClass from within a…
WoodMath
  • 521
  • 2
  • 8
  • 14
9
votes
5 answers

Python - Timeit within a class

I'm having some real trouble with timing a function from within an instance of a class. I'm not sure I'm going about it the right way (never used timeIt before) and I tried a few variations of the second argument importing things, but no luck.…
DizzyDoo
  • 1,489
  • 6
  • 21
  • 32
9
votes
3 answers

Python classes self.variables

I have started learning python classes some time ago, and there is something that I do not understand when it comes to usage of self.variables inside of a class. I googled, but couldn't find the answer. I am not a programmer, just a python…
marco
  • 899
  • 5
  • 13
  • 21
9
votes
2 answers

Why don't monkey-patched methods get passed a reference to the instance?

See this example for a demonstration: >>> class M: def __init__(self): self.x = 4 >>> sample = M() >>> def test(self): print(self.x) >>> sample.test = test >>> sample.test() Traceback (most recent call last): File "",…
temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
9
votes
2 answers

PHP Calling self on a non-static method

Why is the 'self'-call to a non-satic method in this example working? class A{ protected function aNonStaticMethod(){ return __class__; } public function aEcho(){ echo self::aNonStaticMethod(); } } Thanks for…
user2853437
  • 750
  • 8
  • 27
9
votes
1 answer

self.delegate = self; what's wrong in doing that?

self.delegate = self; what's wrong in doing that? and what is the correct way of doing it? Thanks, Nir. Code: (UITextField*)initWith:(id)sender:(float)X:(float)Y:(float)width:(float)hieght:(int)textFieldTag { if (self = [super…
Tiger
  • 383
  • 1
  • 5
  • 13
9
votes
1 answer

Python assigning two variables on one line

class Domin(): def __init__(self , a, b) : self.a=a , self.b=b def where(self): print 'face : ' , self.a , "face : " ,self.b def value(self): print self.a + self.b d1=Domin(1 , 5) d1=Domin(20 , 15) I get…
tabebqena
  • 1,204
  • 1
  • 13
  • 23