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
-2
votes
2 answers

what did i do wrong missing 1 required positional argument: 'self'

class Area(object): def __init__(self, base, height): self.base = base self.height = height def calculation(self): return(self.base * self.height) area = Area(15, 2) print(Area.calculation())
-2
votes
3 answers

setting self variables based on each other

quick question in python let's say my class goes like this class Enemy(): def __init__(self, level): self.level = level self.damage = self.level + 5 the thing is that since the self.damage value is instantiated once it will not…
Yikes
  • 7
  • 5
-2
votes
1 answer

Explanation of self in python

In python's OOPs concept, a instance of class to be made to call other methods. I had a good explanation about self by arjun-sreedharan, how self works. His explanation, Let's say you have a class ClassA which contains a method methodA defined…
M.sainath
  • 15
  • 1
  • 6
-2
votes
2 answers

Do we have to always use self.variable while using them in some expression?

class learn: def __init__(self,radius=1): self.radius = radius def reset_area(self,new_radius): self.radius= new_radius self.area = new_radius*new_radius*3.14 self.circum = 2*3.14*radius return…
Nisha
  • 1
-2
votes
1 answer

Why do I get "NameError: name 'self' is not defined"?

I am using the following code to implement a web server using Flask and socketio. app = Flask(__name__, static_url_path = '', static_folder = 'static', template_folder = 'templates') app.config['SECRET_KEY'] = 'secret!' socketio = SocketIO(app,…
Cristian M
  • 195
  • 1
  • 1
  • 15
-2
votes
1 answer

Why I have two arguments when i call my method?

I wrote a program which work but without using self and now i try to rewrite it with "self". I put some self pretty much everywhere but i have a problem of given argument. First i create a class: class photo(): def _init_cam(self,x,y): …
Hyderman
  • 1
  • 1
-2
votes
1 answer

Python Class with Self - Positional argument errors while calling the class

I am having issues with self, I have written a class to make a data comparison of two datasets which I am feeding in as a csv - while trying to call this function, I am getting a TypeError. Here is my class import csv import time import pandas as…
Ada_lovelace
  • 637
  • 2
  • 6
  • 18
-2
votes
5 answers

missing 1 required positional argument?

I cant get what is wrong with this code? It keeps giving me an error. I do not wanna create a class. It keeps giving me the "missing 1 required positional argument 'choice' in the main function. Anyone have any suggestions? The script is supposed to…
-2
votes
3 answers

python def Method should have "self" as first argument

I’m studying python through the paid online course and i got an error by typing following codes while studying module and pakages. class Fibonacci: def __init__(self, title="fibonacci"): self.title = title def fib(n): a, b =…
Doyeon
  • 7
  • 7
-2
votes
2 answers

When do you need to use self in a nested function?

Why do we need to use self for ans, but not min1 in the following code (via LeetCode)? def findSecondMinimumValue(self, root): self.ans = float('inf') min1 = root.val def dfs(node): if node: if min1 < node.val <…
jlin90
  • 1
  • 1
-2
votes
2 answers

I don't understand how they are initializing current solution. Could someone explain what this does?

I don't understand how a boolean can by multiplied by a length. I'm fairly new to coding def __init__(self, capacity, items): self.currentSolution = [False]*len(items)
J French
  • 187
  • 7
-2
votes
1 answer

How to fix attribute error in a class for python

I can't figure out how to fix this attribute error that occurs whenever I attempt to make a class without using global variables. The error says AttributeError: 'Animal' object has no attribute 'habitat'. The error occurs on line 6: class…
user12653128
-2
votes
2 answers

What does the [something] syntax in Swift mean when working with closures?

Normally when capturing reference to a property in a closure I would do something like this - foo { [weak self] in self?.bar() } But I have also seen this written as - foo { [bar] in bar() } What is meant by [bar] and how is this…
Teddy K
  • 820
  • 1
  • 6
  • 17
-2
votes
1 answer

TypeError: openFile() takes exactly 1 argument (0 given)

Im getting an error "TypeError: openFile() takes exactly 1 argument (0 given) " If i remove the self from the open File method it runs the method and opens the file selector window but then i wont have accress to my other refreshVECs method from…
-2
votes
2 answers

What is the self.val equivalent in functional programming?

If we program in oop way, we're provided flexibility for self.var class Solution: def isValidBST(self, root): self.lastVal = - 2**32 self.isBST = True self.valid(root) return self.isBST def valid(self,…
Pythoner
  • 5,265
  • 5
  • 33
  • 49
1 2 3
99
100