Quantcast
Browsing all 77 articles
Browse latest View live

What is the purpose of the word 'self', in Python?

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...

View Article


Answer by sameer_nubia for What is the purpose of the word 'self', in Python?

self is acting as like current object name or instance of class .# Self explanation. class classname(object): def __init__(self,name): self.name=name # Self is acting as a replacement of object name....

View Article


Answer by nmxl for What is the purpose of the word 'self', in Python?

from the docs, the special thing about methods is that the instance object is passed as the first argument of the function. In our example, the call x.f() is exactly equivalent to MyClass.f(x). In...

View Article

Answer by nmxl for What is the purpose of the word 'self'?

from the docs, the special thing about methods is that the instance object is passed as the first argument of the function. In our example, the call x.f() is exactly equivalent to MyClass.f(x). In...

View Article

Answer by sameer_nubia for What is the purpose of the word 'self'?

self is acting as like current object name or instance of class .# Self explanation. class classname(object): def __init__(self,name): self.name=name # Self is acting as a replacement of object name....

View Article


Answer by prosti for What is the purpose of the word 'self'?

self is inevitable.There was just a question should self be implicit or explicit.Guido van Rossum resolved this question saying self has to stay.So where the self live?If we would just stick to...

View Article

Answer by Bugs Buggy for What is the purpose of the word 'self'?

The use of the argument, conventionally called self isn't as hard to understand, as is why is it necessary? Or as to why explicitly mention it? That, I suppose, is a bigger question for most users who...

View Article

Answer by Akash Kandpal for What is the purpose of the word 'self'?

First of all, self is a conventional name, you could put anything else (being coherent) in its stead.It refers to the object itself, so when you are using it, you are declaring that .name and .age are...

View Article


Answer by user441521 for What is the purpose of the word 'self'?

I'm surprised nobody has brought up Lua. Lua also uses the 'self' variable however it can be omitted but still used. C++ does the same with 'this'. I don't see any reason to have to declare 'self' in...

View Article


Answer by skyking for What is the purpose of the word 'self'?

Is because by the way python is designed the alternatives would hardly work. Python is designed to allow methods or functions to be defined in a context where both implicit this (a-la Java/C++) or...

View Article

Image may be NSFW.
Clik here to view.

Answer by sw123456 for What is the purpose of the word 'self'?

When objects are instantiated, the object itself is passed into the self parameter. Because of this, the object’s data is bound to the object. Below is an example of how you might like to visualize...

View Article

Answer by ytpillai for What is the purpose of the word 'self'?

Python is not a language built for Object Oriented Programming unlike Java or C++. When calling a static method in Python, one simply writes a method with regular arguments inside it. class Animal():...

View Article

Answer by kmario23 for What is the purpose of the word 'self'?

Take a look at the following example, which clearly explains the purpose of selfclass Restaurant(object): bankrupt = False def open_branch(self): if not self.bankrupt: print("branch opened")#create...

View Article


Answer by TheEnglishMe for What is the purpose of the word 'self'?

In the __init__ method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called.self, as a name, is just a convention, call it as you want !...

View Article

Answer by Arjun Sreedharan for What is the purpose of the word 'self'?

Let's say you have a class ClassA which contains a method methodA defined as:def methodA(self, arg1, arg2): # do somethingand ObjectA is an instance of this class.Now when ObjectA.methodA(arg1, arg2)...

View Article


Answer by dan-klasson for What is the purpose of the word 'self'?

It’s there to follow the Python zen “explicit is better than implicit”. It’s indeed a reference to your class object. In Java and PHP, for example, it's called this.If user_type_name is a field on your...

View Article

Answer by Gaurav Nishant for What is the purpose of the word 'self'?

Its use is similar to the use of this keyword in Java, i.e. to give a reference to the current object.

View Article


Answer by ninjagecko for What is the purpose of the word 'self'?

I will demonstrate with code that does not use classes:def state_init(state): state['field'] = 'init'def state_add(state, x): state['field'] += xdef state_mult(state, x): state['field'] *= xdef...

View Article

Answer by Debilski for What is the purpose of the word 'self'?

Let’s take a simple vector class:class Vector: def __init__(self, x, y): self.x = x self.y = yWe want to have a method which calculates the length. What would it look like if we wanted to define it...

View Article

Answer by kame for What is the purpose of the word 'self'?

I like this example:class A: foo = []a, b = A(), A()a.foo.append(5)b.fooans: [5]class A: def __init__(self): self.foo = []a, b = A(), A()a.foo.append(5)b.fooans: []

View Article
Browsing all 77 articles
Browse latest View live