-3

I am learning Python and I've got structed in self keyword.

class Person:
    def __init__(self,gender):
        self.gender=gender
John=Person("Male")
Richa=Person("Female")
print(f"John is {John.gender} and Richa is {Richa.gender}")

Why don't we declare the variable gender in the class Person, where self.gender is pointing ? Like in any other programming languages we used the this keyword

#include <iostream>
#include<string>
using namespace std;
class Person
{
  
  Person(string gender)
  {
  this->gender=gender;
  }
};
int main()
{
    Person john("Male");
    cout<<john.gender;
    return 0;
}

In this we've declared gender variable in class also and this->gender is pointing to something which can be understood, but not in Python.

I know I am mixing two programming languages but I want to understand this.

khelwood
  • 55,782
  • 14
  • 81
  • 108
aniketpant
  • 113
  • 1
  • 8
  • Python is a dynamic programming language, and it *doesn't have variable delcarations*, not like Java. It has type hints, but those aren't exactly the same thing – juanpa.arrivillaga Jan 01 '22 at 16:30
  • 1
    "Like in anyother programming languages we used this keyword" No, not in any other programming language. C++ is an example, but not all languages are C++, C#, and Java. Also note, `self` is *not a keyword*. In Python, that is merely a convention. Instancevs are passed to methods as the *first positional argument*, which you *can* name anything you want (although, you should use `self` if you want to write idiomatic code) – juanpa.arrivillaga Jan 01 '22 at 16:31
  • FWIW, your example is NOT how members are declared in C++. C++ is a typed language so the compiler needs to know the type of each variable at compile time. Python is a scripting language so decides any types at runtime. But I have some sympathy, having made the C++ -> Python transition. The ability for a Python class to create ‘instance variables’ pretty much anywhere took some getting used to. – DS_London Jan 01 '22 at 16:52
  • @DS_London Python is a typed language. It just doesn't use static, nominal typing. – juanpa.arrivillaga Jan 01 '22 at 17:24
  • @juanpa.arrivillaga I take your point. – DS_London Jan 01 '22 at 17:47
  • Thank you @juanpa.arrivillaga – aniketpant Jan 01 '22 at 18:19
  • explanation of self here: https://stackoverflow.com/questions/2709821/what-is-the-purpose-of-the-word-self?rq=1 – Malo Jan 01 '22 at 19:14

3 Answers3

1

From the docs:

Data attributes correspond to “instance variables” in Smalltalk, and to “data members” in C++. Data attributes need not be declared; like local variables, they spring into existence when they are first assigned to.

Yevgeniy Kosmak
  • 3,561
  • 2
  • 10
  • 26
0

When you write class Person you just define an skeleton or a template but every time you make an instance of Person class you're actually calling the __init__ function of the class (the constructor).
So what actually brings a Person instance to life is the __init__ function and it is best practice to define person's attributes there.
Here self works just like this and it refers to the current instance of the class not the class template itself.

Mehdi Seifi
  • 515
  • 11
  • 22
0

In your examples:

self.gender

has the same use as:

this->gender

so it is just you have to get used to "replace" "this->" by "self." and you can access an instance variable value.

The "only strange thing" is that you have to add "self" as the first parameter of each class method also.

Malo
  • 1,233
  • 1
  • 8
  • 25