'classmethod' object is not callable

Question:

I was performing this exercise. The gentleman on Youtube did not have any issue, but I got the following error. Could anyone give me a piece of advice on how to work with classmethods in order to avoid this?
Thanks in advance.

class Employee:
  def __init__(self,first,last,pay,email):
    self.first=first
    self.last=last
    self.pay=pay
    self.email=email
    
@classmethod
def from_str(cls,emp_str):
    first,last,pay=emp_str.split('-')
    return cls(first,last,int(pay))

new_emp=from_str(Employee,emp_str)

The output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/var/folders/9y/ngq521_x3rlc7wgd1c5kwqgh0000gn/T/ipykernel_1551/359192774.py in      <module>
   ----> 1 new_emp=from_str(Employee,emp_str)

TypeError: 'classmethod' object is not callable
Asked By: Ann Che

||

Answers:

from_str is defined as a function, not a method of Employee class (note the identation). So decorating it with @classmethod makes no sense.

Answered By: Klas Š.
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.