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

How do I get a variable from one file into another file

I want to get my class variables from file x and use them in file y. How can I go about doing this? I have seen that you would use from fileName import * but when i use this i get a error "import * only allowed at one module level"
-1
votes
1 answer

Python- how to get list of self variables in a class consist of N-self

Edited: I want to generate N-number of agents. Each agent will have a name, so I create a random name from names and assigned it to class Agent. After I run the model, I want to get the list of my agents name. This is from mesa: import names from…
dya
  • 195
  • 1
  • 2
  • 16
-1
votes
3 answers

Python Class Issue

This code is giving me this error. class Bank: line 117, in Bank main() in main format(z, '10.2f'), format(bank.MakeWithdrawal(self,amount))) AttributeError: 'float' object has no attribute 'MakeWithdrawal' Any idea where I'm going wrong? Thanks…
Apurva Patel
  • 71
  • 1
  • 1
  • 6
-1
votes
2 answers

How to make def statement return an object within a self(...)

class Items(): def Bucket(self): self.cost(5) print(Items.Bucket()) # I want this to return the cost of the item I want this to print the cost of the item listed. In this case a bucket which i want it to return 5. Right now it…
-1
votes
2 answers

Python - Classes - Self Not Defined

Below I'm attempting to make a simple Keygen as a first project. Somewhere I'm getting the error the Self has not been defined. I'm guessing it's probably something easy import random class KeyGenerator(): def __init__(self): length = 0 …
Number28
  • 13
  • 4
-1
votes
1 answer

What use have editing self permission in linux?

I am trying to learn some stuffs about permissions in Linux, but I am not sure I get the use of the first permission which is associated to current user. I watched some tutorials but I didn't find my answer. I suppose permissions in Linux are used…
softwareRat
  • 57
  • 2
  • 7
-1
votes
2 answers

Understanding classes 'self'

In the following example employee is not used in the __init__ function, but we used it in the add_employee function calling self.employee.append(). Why is that? Why did we use self.employee.append() instead of employee.append() ? I thought we only…
zou
  • 1
  • 1
-1
votes
3 answers

mutliple entry widgets and one button widget in tkinter

I am trying to make two entry boxes and an 'OK' button that processes both entries: I don't understand the init() function or the reason 'self' has to be include in doing this. I want to access the entries outside this class and mainloop. The…
Michael
  • 223
  • 1
  • 2
  • 8
-1
votes
2 answers

self inside block called by super

In this case, weakself is needed to avoid retain cycle [self showMethodA:^{ [weakself showMethodB]; }]; Will this case cause a retain cycle? [super showMethodA:^{ [self showMethodB]; }];
Ted
  • 22,696
  • 11
  • 95
  • 109
-1
votes
2 answers

JS this and self don't work

As seen here How to access the correct `this` context inside a callback? I try to use self instead of this. It's a little bit a stupid question about JS but I would like some explanations and what should I do to get it right. (function (global) { …
-1
votes
1 answer

Why isn't self working in my code

I'm writing some code to create a toolbar that edits a map in ArcMap and I'm having some issues with getting variable values from other functions inside other classes that I'm using. All the functions are predefined so I can't change the int…
user5451396
-1
votes
1 answer

Python: Class variables are not updated

Hello fellow community, I cannot figure out the problem here and therefore would like to have some imput from your side. Here is my code: from collections import defaultdict class peptide(): aa_lib={ 'A': {'H': 5, 'C': 3, 'O': 1,…
Fourier
  • 2,795
  • 3
  • 25
  • 39
-1
votes
2 answers

attribute has no self (but it is not a class attribute either)

I'm new to python and I've been trying to understand classes. As far as I know, when you create a class, you must insert self before a variable, so that self is replaced by the instances, when they're created (these become instance…
Acla
  • 141
  • 3
  • 12
-1
votes
2 answers

How to call a stat from a class into a function - Python

I am struggling to create a health/anger (variables) drop when making the user fight a knight. I have defined a class called kingsmen, that is very simple and used for each knight, but I keep getting told that I am not calling the class properly.…
-1
votes
2 answers

Error referencing self in a dict variable (python)

I'm a bit new to Python and can't figure this one out. I'm not able to debug a similar error in some code I'm writing. I replicated it below. Why is it when I reference self.method() in a dict in a global variable, I get a name error? Error below: $…
user1601871
  • 91
  • 1
  • 6