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
74
votes
6 answers

Python calling method without 'self'

So I just started programming in python and I don't understand the whole reasoning behind 'self'. I understand that it is used almost like a global variable, so that data can be passed between different methods in the class. I don't understand why…
Synaps3
  • 1,597
  • 2
  • 15
  • 23
74
votes
1 answer

Is it okay to pass self to an external function

I have a class, A, which is inherited by a bunch of other classes. Some of these have a few functions which are similar and it would be nice to have those functions defined somewhere else and called by the classes that need them. But those functions…
elssar
  • 5,651
  • 7
  • 46
  • 71
73
votes
6 answers

Calling [self methodName] from inside a block?

I've just run into blocks and I think they are just what I'm looking for, except for one thing: is it possible to call a method [self methodName] from within a block? This is what I'm trying to do: -(void)someFunction{ Fader* fader = [[Fader…
Marty
  • 1,077
  • 1
  • 10
  • 14
69
votes
4 answers

How to use Ruby's self keyword

From what I understand about self, it refers to the current instance of the class. Isn't this the default behaviour at all times anyways? For example, isn't self.var_one = method(args) equivalent to var_one = method(args) If so, what is the use…
ankit
  • 3,328
  • 3
  • 26
  • 39
63
votes
2 answers

What is the difference between class and instance variables?

What is the difference between class and instance variables in Python? class Complex: a = 1 and class Complex: def __init__(self): self.a = 1 Using the call: x = Complex().a in both cases assigns x to 1. A more in-depth answer…
Christopher Markieta
  • 5,674
  • 10
  • 43
  • 60
55
votes
6 answers

In Go is naming the receiver variable 'self' misleading or good practice?

I have seen a fair amount of blogs & videos on Go and as far as I recall, none of the authors use 'self' or 'this' for the receiver variable when writing methods. However there seems to be a number of questions on stack overflow that do this, and…
miltonb
  • 6,905
  • 8
  • 45
  • 55
50
votes
1 answer

Why isn't self always needed in ruby / rails / activerecord?

In testing a getter/setter pair in a rails model, I've found a good example of behavior I've always thought was odd and inconsistent. In this example I'm dealing with class Folder < ActiveRecord::Base. Folder belongs_to :parent, :class_name =>…
Andrew
  • 42,517
  • 51
  • 181
  • 281
48
votes
10 answers

What is "self" used for in Swift?

I am new to Swift and I'm wondering what self is used for and why. I have seen it in classes and structures but I really don't find them essential nor necessary to even mention them in my code. What are they used for and why? In what situations it's…
47
votes
3 answers

How can I decorate an instance method with a decorator class?

Consider this small example: import datetime as dt class Timed(object): def __init__(self, f): self.func = f def __call__(self, *args, **kwargs): start = dt.datetime.now() ret = self.func(*args, **kwargs) …
Rafael T
  • 15,401
  • 15
  • 83
  • 144
45
votes
4 answers

Is there a generic way for a function to reference itself?

I can access a python function's attribute inside of function itself by below code: def aa(): print aa.__name__ print aa.__hash__ # other simliar However, if above aa() function is a template for write other code, say bb(), I have to…
user478514
  • 3,859
  • 10
  • 33
  • 42
42
votes
5 answers

How to call an Objective-C Method from a C Method?

I have an Obj-C object with a bunch of methods inside of it. Sometimes a method needs to call another method inside the same object. I can't seem to figure out how to get a C method to call a Obj-C method... WORKS: Obj-C method calling an Obj-C…
Dave
  • 12,408
  • 12
  • 64
  • 67
39
votes
3 answers

When to use `self.foo` instead of `foo` in Ruby methods

This is not specific for Rails - I am just using Rails as an example. I have a model in Rails: class Item < ActiveRecord::Base def hello puts "Hello, #{self.name}" end end (Let's assume that the Item model (class) has a method called…
jriff
  • 1,947
  • 3
  • 23
  • 33
38
votes
4 answers

When do you use 'self' in Python?

Are you supposed to use self when referencing a member function in Python (within the same module)? More generally, I was wondering when it is required to use self, not just for methods but for variables as well.
Dark Templar
  • 1,129
  • 3
  • 11
  • 18
38
votes
3 answers

TypeError: generatecode() takes 0 positional arguments but 1 was given

I have the code below: from tkinter import * class Window(Frame): def __init__(self, master = None): Frame.__init__(self, master) self.master = master self.init_window() def init_window(self): …
Jason Martin
  • 578
  • 1
  • 4
  • 13
37
votes
6 answers

What is the purpose of checking self.__class__?

What is the purpose of checking self.__class__ ? I've found some code that creates an abstract interface class and then checks whether its self.__class__ is itself, e.g. class abstract1 (object): def __init__(self): if self.__class__ ==…
alvas
  • 115,346
  • 109
  • 446
  • 738