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

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 all 77 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>