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


Messages In This Thread
creating a class. - by prathmesh - Oct-26-2018, 01:06 PM
RE: creating a class. - by buran - Oct-26-2018, 01:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,956 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Help creating a class instance dynamically Kotevski 9 5,484 Aug-17-2018, 05:23 AM
Last Post: Gribouillis
  Creating class - name not defined error python_alex 3 16,432 Jun-30-2018, 11:17 PM
Last Post: snippsat
  Creating a list of class objects hjuyrfc 2 3,116 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