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 Ponkadoodle for What is the purpose of the word 'self'?

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 word 'self'?

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 word 'self'?

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 word 'self'?

The reason you need to use self. is because Python does not use the @ 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 word 'self'?

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

View Article


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

View Article

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

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 Rahul Jha for What is the purpose of the word 'self'?

"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 PYC for What is the purpose of the word 'self'?

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 word 'self'?

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