WalidKhan Posted April 26, 2023 Share Posted April 26, 2023 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! Link to comment Share on other sites More sharing options...
Sensei Posted April 26, 2023 Share Posted April 26, 2023 "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. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now