Why we use self in python


Asked by wiki @ & viewed by 116 persons


What is the purpose of the word 'self'?

What is the purpose of the self word in Python? I understand it refers to the specific object created from that class, but I can't see why it explicitly needs to be added to every function as a parameter. To illustrate, in Ruby I can do this:

class myClass
    def myFunc(name)
        @name = name
    end
end

Which I understand, quite easily. However in Python I need to include self:

class myClass:
    def myFunc(self, name):
        self.name = name

Can anyone talk me through this? It is not something I've come across in my (admittedly limited) experience.

Do you know the better answer?

Similar questions

What is the use of self in python

Asked by wiki @ & viewed by 74 persons

What is the purpose of the word 'self'? What is the purpose of the self word in Python? I understand …

Why self is used in python

Asked by wiki @ & viewed by 73 persons

What is the purpose of the word 'self'? What is the purpose of the self word in Python? I understand …

Use of self in python

Asked by wiki @ & viewed by 71 persons

What is the purpose of the word 'self'? What is the purpose of the self word in Python? I understand …

How to create a 2d array in python using numpy

Asked by wiki @ & viewed by 7 persons

How to generate 2d numpy array? I'm trying to generate a 2d numpy array with the help of generators: x …

How to use py2exe python 3.7

Asked by wiki @ & viewed by 10 persons

py2exe - generate single executable file I thought I heard that py2exe was able to do this, but I never …

Deblur image using opencv python

Asked by wiki @ & viewed by 6 persons

How to motion deblur an image using OpenCV and Python? So I have been asked to motion deblur a frame …

Python use cmd commands

Asked by wiki @ & viewed by 8 persons

How to execute a command prompt command from python I tried something like this, but with no effect: command = …

Automating desktop applications using python

Asked by wiki @ & viewed by 6 persons

Python code to automate desktop activities in windows I want to automate desktop activities in Windows environment using Python. How …

How to use all python

Asked by wiki @ & viewed by 6 persons

How do Python's any and all functions work? I'm trying to understand how the any() and all() Python built-in functions …

Palindrome using for loop in python

Asked by wiki @ & viewed by 9 persons

How to check for palindrome using while loop python I'm trying to check for a palindrome using a while loop …

How to use ffmpeg in python

Asked by wiki @ & viewed by 7 persons

ffmpeg in python script I would like to run the following command in a python script, I also want to …

How to use valueerror in python

Asked by wiki @ & viewed by 5 persons

python: Should I use ValueError or create my own subclass to handle invalid strings? [duplicate] This question already has answers …

Python use function as variable

Asked by wiki @ & viewed by 9 persons

Assigning a function to a variable Let's say I have a function def x(): print(20) Now I want to assign …

Addition in python using function

Asked by wiki @ & viewed by 6 persons

Write a program to find sum of two numbers using functions and calculate their average? I understand the questions but …

Which marker is used to get circles in python

Asked by wiki @ & viewed by 7 persons

Matplotlib; Scatter plot marker, dot within circle I'm plotting using the Matplotlib scatter plotter. For the markers I'd ideally like …

Most viewed questions


Python strftime milliseconds

Asked by wiki @ & viewed by 200 persons

Format a datetime into a string with milliseconds I want to have a datetime string from the date with milliseconds. …

What does the percent sign mean in python

Asked by wiki @ & viewed by 136 persons

What does the percentage sign mean in Python In the tutorial there is an example for finding prime numbers: >>> …

Self keyword in python

Asked by wiki @ & viewed by 134 persons

What is the purpose of the word 'self'? What is the purpose of the self word in Python? I understand …

Check object type python

Asked by wiki @ & viewed by 132 persons

Determine the type of an object? Is there a simple way to determine if a variable is a list, dictionary, …

Correlation matrix python

Asked by wiki @ & viewed by 131 persons

Plot correlation matrix using pandas I have a data set with huge number of features, so analysing the correlation matrix …

Alphabetical sort python

Asked by wiki @ & viewed by 131 persons

Python data structure sort list alphabetically I am a bit confused regarding data structure in python; (),[], and {}. I …

Python random

Asked by wiki @ & viewed by 127 persons

Generate random integers between 0 and 9 How can I generate random integers between 0 and 9 (inclusive) in Python? …

Python create dataframe from list

Asked by wiki @ & viewed by 124 persons

Python: create a pandas data frame from a list I am using the following code to create a data frame …

Python one line for loop

Asked by wiki @ & viewed by 123 persons

Python one-line “for” expression I'm not sure if I need a lambda, or something else. But still, I need the …

How to add a column to dataframe python

Asked by wiki @ & viewed by 123 persons

Adding new column to existing DataFrame in Python pandas I have the following indexed DataFrame with named columns and rows …

Why we use self in python

Asked by wiki @ & viewed by 116 persons

What is the purpose of the word 'self'? What is the purpose of the self word in Python? I understand …

Shape 0 python

Asked by wiki @ & viewed by 115 persons

x.shape[0] vs x[0].shape in NumPy Let say, I have an array with x.shape = (10,1024) when I try to print …

Python print working directory

Asked by wiki @ & viewed by 112 persons

Find current directory and file's directory [duplicate] This question already has answers here: How do you properly determine the current …

Image shape python

Asked by wiki @ & viewed by 110 persons

Python OpenCV2 (cv2) wrapper to get image size? How to get the size of an image in cv2 wrapper in …

Interface grafica python

Asked by wiki @ & viewed by 108 persons

Como criar interface gráfica em Python? [duplicada] Essa pergunta já tem uma resposta aqui: É possível criar interfaces gráficas para …