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.