Jump to content

Understanding 'Self' and Python instance creation

Featured Replies

I'm attempting to understand 'self' in Python on a deeper level since I find myself not fully comprehending it when I use it.

Consider the following code:

class FoodItems:

def __init__(self, other arguments):
some code

def main():
some code

item1 = FoodItems
item1.main()

For the [def main()] function I'm not passing the 'Self' argument in it, yet the code works when I call it. After reading this post, my understanding of 'Self' is that it must be provided every time a method is defined under the class. I receive the error (missing 1 needed positional argument:'self') if I pass 'Self' and then call the method.

Could someone please clarify this to me? When it comes to generating instances, I can't discern the difference between calling an instance with or without brackets, such as (item1 = FoodItems vs. item1=FoodItems()).

When I use parentheses, I receive the error "missing 8 required positional arguments that were initialized in the constructor."

Any assistance would be highly appreciated; thank you in advance!
 

"self" does not have to be called self in Python. It can be anything that is not a reserved word.

"self" is the Python equivalent of "this" from other languages such as C++ or Java:

https://en.wikipedia.org/wiki/This_(computer_programming)

 

In other words, it is a pointer/holder/reference to the object's data. Each new instance will have a different set of data, pointed to by "this"/"self".

 

https://www.w3schools.com/python/gloss_python_self.asp

class Person:
  def __init__(mysillyobject, name, age):
    mysillyobject.name = name
    mysillyobject.age = age

  def myfunc(abc):
    print("Hello my name is " + abc.name)

p1 = Person("John", 36)
p1.myfunc() 

"mysillyobject" and "abc" are equal to "p1" in the above code.

 

The above code in C++ could look like:

#include <iostream>

class Person {
 private:
  std::string name;
  int age;
 public: Person( const std::string name, int age ) {
  this->name = name;
  this->age = age;
 }
 public: void myfunc() {
  std::cout << "Name: " << this->name << std::endl;
  std::cout << "Age: " << this->age << std::endl;
 }
};

int main() {
 Person *p = new Person( "John", 36 );
 p->myfunc();
 return( 0 );
}

C++ allows you to omit "this". "this" is the default for the object's local variables (i.e. name and age). But since I used these words as constructor parameters, I have to use "this" to differentiate them.

I could write:


 public: void myfunc() {
  std::cout << "Name: " << name << std::endl;
  std::cout << "Age: " << age << std::endl;
 }

And the C++ compiler would know that "name" is equivalent to "this->name", and "age" is equivalent to "this->age".

It's different in Python and "self" is obligatory.

 

Please sign in to comment

You will be able to leave a comment after signing in

Sign In Now

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.