Rear&str problems

class Employee:
    raise_amt = 1.04
    num_of_emps = 0
    def __init__(self, first, last, pay):
        self.first = first
        self.last = last
        self.pay = pay
        self.email = first + "." + last + "@company.com"
    def fullname(self):
        return f"{self.first} {self.last}"
    def __repr__(self):
        return f"Employee.{self.first} {self.last} {self.pay}"
    def __str__(self):
        return f"{self.fullname}"

emp_1 = Employee("Corey", "Schafer", 50000)
print(emp_1)
print(str(emp_1))

the result is :

<bound method Employee.fullname of Employee.Corey Schafer 50000>
<bound method Employee.fullname of Employee.Corey Schafer 50000>

OBViOUSLY, the result of print(str(emp_1)) should be Corey Schafer, rather than: <bound method Employee.fullname of Employee.Corey Schafer 50000>

so, would you guys like to help me solve this problem?
Thanks.

def __str__(self):
  return f"{self.email}"

but I do this above action, everything seems works fine. so this must be a problem related to the fullname method.

The dunder init shows how to print those. You need:
print(emp_1.first, emp_1.last, emp_1.pay)

Its not entirely clear if you are experimenting with repr and str, or whether your issue is with the object instantiation.

If it’s the latter L.A.P is right as you are creating and object with attributes but not calling them. Even though you’ve provided the attributes for that Employee, you’ve not mentioned them when printing. It’s like saying ‘create an Employee and then print them’. But Python needs ‘create an Employee and print their full name’.

I suspect you actually wanted to call the functions you’ve defined to manage these attributes. So again you need to tell Python to ‘print the Employee and the function that defines their full name’. (That’s not technically accurate but you get the idea).

class Employee():                                                           
                                                                     
    raise_amt = 1.04                                                        
    num_of_emps = 0                                                         
                                                                                 
    def __init__(self, first, last, pay):                                   
        self.first = first                                                  
        self.last = last                                                    
        self.pay = pay                                                      
        self.email = first + "." + last + "@company.com"                    
                                                                               
    def fullname(self):
        return f"{self.first} {self.last}"                                  
                                                                               
   def __repr__(self):                                                     
       return f"{self.first} {self.last} {self.pay}"                       
                                                                                
   def __str__(self):                                                      
       return f"{self.fullname}"                                           
                                                                                
                                                                                
emp_1 = Employee("Corey", "Schafer", 50000)                                 
print(emp_1.fullname())                                                     
print(emp_1.__repr__())                                                     

Given what you are trying to do, you might want to consider a dict data structure with key, value pairs. I’ve appended it here but it would be best to deal with it in the init

emp_2 = Employee("John", "Doe", 15000)                                      
emp_dict = { "First Name": emp_2.first, "Last Name": emp_2.last,            
                   "Pay": emp_2.pay, "Email": emp_2.email }   

Then print the value via the key

print(emp_dict["Email"])   
2 Likes
#https://www.youtube.com/watch?v=3ohzBxoFHAY
class Employee:
raise_amt = 1.04
num_of_emps = 0
def __init__(self, first, last, pay):
    self.first = first
    self.last = last
    self.pay = pay
    self.email = first + "." + last + "@company.com"
    self.fullname = f"{first} {last}"
#    def fullname(self):
#        return f"{Employee.first} {Employee.last}"

def __repr__(self):
   return f"Employee.{self.first} {self.last} {self.pay}"
def __str__(self):
  return f"{self.fullname}"

emp_1 = Employee("Corey", "Schafer", 50000)
print(emp_1)
print(repr(emp_1))
print(str(emp_1))

the Terminal result is this:

Corey Schafer
Employee.Corey Schafer 50000
Corey Schafer

as you can see. what I want is:

def __str__(self):
  return f"{self.fullname}"

above this two line works.
why I have to put the fullname under the init method, rather than create a new method. this is what I am wondering.

1 Like

You don’t… Look at my example. Its a new method under that Employee class.

1 Like

I think people on the thread are close but it’s simply this:

return f"{self.fullname()}"

Rather than what you have which is:

return f"{self.fullname}"

See the difference? With the two tiny little () after fullname python doesn’t know that you want to call the fullname function. Python thinks you want to do something with the function and went and got it for you.

1 Like

many many thanks for the great examples