{"id":28254,"date":"2022-08-29T00:19:11","date_gmt":"2022-08-28T18:49:11","guid":{"rendered":"https:\/\/python-programs.com\/?p=28254"},"modified":"2022-08-29T00:19:11","modified_gmt":"2022-08-28T18:49:11","slug":"self-in-python-class","status":"publish","type":"post","link":"https:\/\/python-programs.com\/self-in-python-class\/","title":{"rendered":"self in Python class"},"content":{"rendered":"
In this article, let us look at what is a self in python class along with some examples to understand the self<\/strong> more clearly.<\/p>\n When we write a class in Python, we need a way to access its attributes and methods. Most languages have a fixed syntax for referring to attributes and methods; C++, for example, uses this for reference.<\/p>\n What is self in Python class?<\/strong><\/p>\n The self in class is used to represent the instance of the class. In Python, you can access the attributes and methods of the class using this keyword. It binds the attributes with the arguments provided. We use self because Python does not use the ‘@’ syntax to refer to instance attributes. In Python, there are methods that cause the instance to be passed but not received automatically.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n NOTE<\/strong><\/p>\n In function, self is a parameter that can be replaced with a different parameter name.<\/p>\n Here we replace self with hello <\/strong>in the above example. Still, it works fine.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The Instance method and constructor, self <\/strong>must be provided as a First parameter. It will result in an error if you do not provide it.<\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In this article, let us look at what is a self in python class along with some examples to understand the self more clearly. When we write a class in Python, we need a way to access its attributes and methods. Most languages have a fixed syntax for referring to attributes and methods; C++, for …<\/p>\nSelf is always points to the Current Object<\/pre>\n
self in Python class<\/h2>\n
\n
Showing the address of self and object<\/h3>\n
\n
# Here we can observe that the self and rslt_object is referring to the same object\r\n# Create a class say checkSelf\r\nclass checkSelf:\r\n # In the constructor we print the value of self\r\n def __init__(self):\r\n print(\"The Address\/Id of self: \", id(self))\r\n \r\n# Create an object for the above class (Here self referes to the this object)\r\nrslt_object = checkSelf()\r\n# Print the id of the above created about using the id() function\r\nprint(\"The Address\/Id of a checkSelf class object: \",id(rslt_object))<\/pre>\n
The Address\/Id of self: 139973697862096\r\nThe Address\/Id of a checkSelf class object: 139973697862096<\/pre>\n
Here we can observe that the self and rslt_object is referring to the\r\nsame object. We can see the address of the self and the address of \r\nthe object are the same.<\/pre>\n
Method #1: Using self to call the methods<\/h3>\n
\n
# Create a class say Employ\r\nclass Employ():\r\n\r\n # Create a init constructor by passing self, employName, employId as arguments to it\r\n def __init__ (self, employName, employId):\r\n # Initilaize the instance variables\r\n self.employName = employName\r\n self.employId = employId\r\n\r\n # Create a method say showEmployId which prints the employ id using self\r\n def showEmployId(self):\r\n print(\"Employ Id = \", self.employId)\r\n \r\n # Create another method say showEmployName which prints the employ name using self\r\n def showEmployName(self):\r\n print(\"Employ Name = \", self.employName)\r\n \r\n # Create a method say showEmployDetails which calls both the methods using self\r\n # showEmployName, showEmployId\r\n def showEmployDetails(self):\r\n self.showEmployName()\r\n self.showEmployId()\r\n\r\n# Create an Instance\/object of the above created Employ class \r\nemployObj = Employ(\"John\", 2122)\r\n\r\n# Call the above method showEmployDetails() using the above employ Object\r\nemployObj.showEmployDetails()<\/pre>\n
Employ Name = John\r\nEmploy Id = 2122<\/pre>\n
\n
The term self is not a keyword; rather, it is a Python developer convention.\r\nYou can substitute any other word for the first parameter and it will \r\nstill work. However, it is preferable to use self rather than anything \r\nelse because it enhances code readability.<\/pre>\n
Method #2: Replacing self with some other name<\/h3>\n
\n
# Create a class say Employ\r\nclass Employ():\r\n\r\n # Create a init constructor by passing employName, employId as \r\n # arguments to it using hello instead of self\r\n def __init__ (hello, employName, employId):\r\n # Initilaize the instance variables\r\n hello.employName = employName\r\n hello.employId = employId\r\n\r\n # Create a function say showEmployId which prints the employ id\r\n # using hello instead of self\r\n def showEmployId(hello):\r\n print(\"Employ Id = \", hello.employId)\r\n \r\n # Create another function say showEmployName which prints the employ name\r\n # using hello instead of self\r\n def showEmployName(hello):\r\n print(\"Employ Name = \", hello.employName)\r\n \r\n # Create a function say showEmployDetails which calls both the functions \r\n # showEmployName, showEmployId using hello instead of self\r\n def showEmployDetails(hello):\r\n hello.showEmployName()\r\n hello.showEmployId()\r\n\r\n# Create an Instance\/object of the above created Employ class \r\nemployObj = Employ(\"John\", 2122)\r\n\r\n# Call the above function showEmployDetails() using the above employ Object\r\nemployObj.showEmployDetails()<\/pre>\n
Employ Name = John\r\nEmploy Id = 2122<\/pre>\n
Method #3: Without Providing self Parameter<\/h3>\n
# Here we can observe that the self is not passed as the first parameter\r\n# in the init constructor. Hence it throws an error\r\n\r\n# Create a class say checkwithoutSelf\r\nclass checkwithoutSelf:\r\n # In the init constructor we don't pass the self \r\n def __init__():\r\n print(\"hello this is python programs\")\r\n\r\n# Create an object for the above class \r\nrslt_object = checkwithoutSelf()\r\nprint(\"Welcome\")<\/pre>\n
---------------------------------------------------------------------------\r\nTypeError Traceback (most recent call last)\r\n<ipython-input-43-394e0a56a3d8> in <module>\r\n9 \r\n10 # Create an object for the above class\r\n---> 11 rslt_object = checkwithoutSelf()\r\n12 print(\"Welcome\")\r\n\r\nTypeError: __init__() takes 0 positional arguments but 1 was given<\/pre>\n