Python Forum
Trying to understand the python code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to understand the python code
#1
Hello,
I am a beginner. I have written this code and it works for some part while it doesn't work the other time. Kindly help.

class Employee:
    def __init__(self, full_name, age ):
        self.full_name = full_name
        self.age = age

    def shout(self):
        print(f"the details of employee is , {self.full_name} and the age is {self.age}")

    def run(self):
        return self


empl_1 = Employee("Suman Palisetty", 37)
# empl_2 = Employee("Suman Palisetty", 38)
# empl_1.shout()
# empl_2.shout()


print(empl_1.run())                 #It gives me <__main__.Employee object at 0x000002952C978400>
# print(Employee.run())             '''if we uncomment this line and run it, it gives me this error.
Error:
Traceback (most recent call last): File "C:/Users/spali/PycharmProjects/First/decorator1.py", line 18, in <module> print(Employee.run()) TypeError: run() missing 1 required positional argument: 'self' '''
What am I doing wrong, why it doesn't work for me?

</code>
Reply
#2
Your Employee.run() method returns self - that is the instance of the class. So with print(empl_1.run()) you get <__main__.Employee object at 0x000002952C978400> what is expected.

run() is instance method, it expects to get [automatically] instance of the class as firs argument self. If you run print(Employee.run()) run it does not get such instance and you get the error.

Now it's unclear what you actually expect run() method to do. Care to elaborate, so that we can be of more help?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
The way to use classes are via objects. This means that Employee is just like a name for your class, like a title. The way you use the class is by initializing an object. This is done by this code:
empl_1 = Employee("Suman Palisetty", 37)
. This makes empl_1 an object from the class Employee, meaning to say that empl_1 is able to use the methods defined in the Employee class. However, the Employee class itself is not meant to be called to use a method.

I'll give you another example:
Lets say I create a class called Money with initializer method def __init__(self, value)
And lets say I create a method called deposit() that deposits the money
I will initialize an object called Five dollar note by doing Five_Dollar_Note = Money(5) #Assigning value of 5
I will also initialize another object called Ten dollar note by doing Ten_Dollar_Note = Money(10) #Assigning value of 10
To use my deposit function, I will do Five_Dollar_Note.deposit() or Ten_Dollar_Note.deposit() instead of Money.Deposit()
because you can deposit a 5 dollar note, or a 10 dollar note, but you don't deposit "money". However, 5 dollar note and 10 dollar note are both "money"
I don't know if I did a good job explaining it but I hope you understand.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to understand the meaning of the line of code. jahuja73 0 309 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Understand what it means that everything in Python is an object... bytecrunch 8 3,821 Mar-19-2021, 04:47 PM
Last Post: nilamo
  Understand order of magnitude performance gap between python and C++ ThelannOryat 4 2,720 Mar-17-2021, 03:39 PM
Last Post: ThelannOryat
  Unable to understand how given code takes a fixed input value, without inputting. jahuja73 4 2,711 Jan-28-2021, 05:22 PM
Last Post: snippsat
  Don't understand example code from a textbook lil_fentanyl 1 1,838 Jan-25-2021, 07:02 PM
Last Post: nilamo
  Unable to understand a statement in an existing code ateestructural 1 2,231 Aug-01-2020, 09:38 PM
Last Post: deanhystad
  I couldn't understand the output of the below code ravich129 1 1,932 Dec-12-2019, 06:24 AM
Last Post: sandeep_ganga
  I do not understand why my python looks different from tutorials. noodlespinbot 2 5,139 Oct-12-2019, 09:56 PM
Last Post: noodlespinbot
  I dont understand bytes in python. blackknite 3 4,019 Oct-02-2019, 07:39 PM
Last Post: Gribouillis
  can you understand why this code prints None? arcbal 2 2,762 Mar-13-2019, 02:57 AM
Last Post: arcbal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020