Quantcast
Channel: What is the purpose of the `self` parameter? Why is it needed? - Stack Overflow
Browsing latest articles
Browse All 77 View Live

Answer by PrabhavDevo for What is the purpose of the `self` parameter? Why is...

The word 'self' refers to instance of a classclass foo: def __init__(self, num1, num2): self.n1 = num1 #now in this it will make the perimeter num1 and num2 access across the whole class self.n2 = num2...

View Article



Answer by saran for What is the purpose of the `self` parameter? Why is it...

my little 2 centsIn this class Person, we defined out init method with the self and interesting thing to notice here is the memory location of both the self and instance variable p is same...

View Article

Answer by Rahul Jha for What is the purpose of the `self` parameter? Why is...

"self" keyword holds the reference of class and it is upto you if you want to use it or not but if you notice, whenever you create a new method in python, python automatically write self keyword for...

View Article

Answer by Rishi for What is the purpose of the `self` parameter? Why is it...

I would say for Python at least, the self parameter can be thought of as a placeholder.Take a look at this:class Person: def __init__(self, name, age): self.name = name self.age = agep1 =...

View Article

Answer by laxman for What is the purpose of the `self` parameter? Why is it...

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 `self` parameter? Why...

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 `self` parameter? Why is it...

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 `self` parameter? Why is...

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 `self` parameter? Why...

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 `self` parameter? Why is...

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 `self` parameter? Why is it...

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 `self` parameter? Why is it...

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 rassa45 for What is the purpose of the `self` parameter? Why is it...

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 `self` parameter? Why is it...

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 Oussama L. for What is the purpose of the `self` parameter? Why is...

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 `self` parameter?...

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 `self` parameter? Why is...

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 `self` parameter? Why...

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 `self` parameter? Why is...

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 `self` parameter? Why is it...

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 `self` parameter? Why is it...

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


Answer by Ponkadoodle for What is the purpose of the `self` parameter? Why is...

As well as all the other reasons already stated, it allows for easier access to overridden methods; you can call Class.some_method(inst).An example of where it’s useful:class C1(object): def...

View Article


Answer by Matthew Rankin for What is the purpose of the `self` parameter? Why...

The following excerpts are from the Python documentation about self:As in Modula-3, there are no shorthands [in Python] for referencing the object’s members from its methods: the method function is...

View Article

Answer by Ming-Tang for What is the purpose of the `self` parameter? Why is...

self is an object reference to the object itself, therefore, they are same.Python methods are not called in the context of the object itself.self in Python may be used to deal with custom object models...

View Article

Answer by Thomas Wouters for What is the purpose of the `self` parameter? Why...

The reason you need to use self. is because Python does not use special syntax to refer to instance attributes. Python decided to do methods in a way that makes the instance to which the method belongs...

View Article


Answer by SilentGhost for What is the purpose of the `self` parameter? Why is...

it's an explicit reference to the class instance object.

View Article

What is the purpose of the `self` parameter? Why is it needed?

Consider this example:class MyClass: def func(self, name): self.name = nameI know that self refers to the specific instance of MyClass. But why must func explicitly include self as a parameter? Why do...

View Article
Browsing latest articles
Browse All 77 View Live




Latest Images