Python Forum

Full Version: creating a class.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am trying to create a new class with the following code :
class employee:
   
   def __init__(self, first, sal):
     self.first = first
     self.sal = sal
   

   
emp_1 = employee('rahul', 10000)


print(emp_1)
But when I am run it, instead of displaying the first and sal it gives the following output :
<__main__.employee object at 0x0000008500B7A748>

Kindly guide.Thanks!!
you need to implement some special methods - e.g. __str__ and/or __repr__

class Employee:
    def __init__(self, first, sal):
        self.first = first
        self.sal = sal
    
    def __str__(self):
        return 'Employee {}, salary {}'.format(self.first, self.sal)
    
employee = Employee('rahul', 10000)

print(employee)
Output:
Employee rahul, salary 10000
Read this https://stackoverflow.com/a/2626364/4046632 for some more information and explanation.