Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
creating a class.
#1
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!!
Reply
#2
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.
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,932 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Help creating a class instance dynamically Kotevski 9 5,388 Aug-17-2018, 05:23 AM
Last Post: Gribouillis
  Creating class - name not defined error python_alex 3 16,321 Jun-30-2018, 11:17 PM
Last Post: snippsat
  Creating a list of class objects hjuyrfc 2 3,089 Jul-22-2017, 07:41 PM
Last Post: hjuyrfc

Forum Jump:

User Panel Messages

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