0

I get a Traceback Type Error Employee() takes no arguments, when trying to create a class method to parse string and create object in python

  class Employee(object):
    @classmethod
    def from_string(cls, emp_str):
        first_name, last_name, pay = emp_str.split('-')
        return cls(first_name, last_name, pay)**

    emp_str_1 = 'Larry-David-100000'
    new_emp_1 = Employee.from_string(emp_str_1)
    print(new_emp_1.first_name, new_emp_1.last_name, new_emp_1.pay)

Traceback (most recent call last):
  File "C:\class_method.py", line 14, in <module>
    new_emp_1 = Employee.from_string(emp_str_1)
  File "C:\class_method.py", line 8, in from_string
    return cls(first_name, last_name, pay)
TypeError: Employee() takes no arguments
John Gordon
  • 29,573
  • 7
  • 33
  • 58
devguygo
  • 1
  • 2
  • You didn't define an `__init__()` method for the class, so it is using the default init which takes no parameters. – John Gordon Dec 02 '20 at 00:43
  • Does this answer your question? [TypeError: Car() takes no arguments](https://stackoverflow.com/questions/62626731/typeerror-car-takes-no-arguments) – Gino Mempin Dec 02 '20 at 00:44
  • I thought only instance methods need __init__ and this is not required for class methods. – devguygo Dec 02 '20 at 00:47
  • Your `cls(first_name, last_name, pay)` is going to call the class' `__init__`, because it's going to instantiate an object, just like `Employee(first_name, last_name, pay)`. So you still need an `__init__` function that accepts those parameters. – Gino Mempin Dec 02 '20 at 00:50
  • If your classmethod is *not* instantiating an Employee object, then yes `__init__` is not needed. – Gino Mempin Dec 02 '20 at 01:00
  • How can I run the @classmethod without instantiating an Employee object? Because I keep getting the TypeError Employee() take no arguments error. – devguygo Dec 02 '20 at 01:36
  • 1
    The problem is not with the classmethod itself; it's because the classmethod calls `cls(first_name, last_name, pay)`. What do you expect that to even do? Within your Employee class, where do you expect `first_name`, `last_name`, `pay` to be handled? – John Gordon Dec 02 '20 at 01:47
  • I am trying to implement the @classmethod to first parse the string and then as an alternative constructor to the __init__ method. I read that the classmethod can also be used as an alternative constructor to the regular instance method. – devguygo Dec 02 '20 at 06:51
  • I don't know what you read but it could be missing some information or examples. A class needs to call `__init__` to instantiate the class. And your use-case here is instantiating the class with `cls(...)`. Where else is it supposed to store `.first_name` and `.last_name` for every Employee object. Using a @classmethod to construct an object is useful for factory design patterns like this https://stackoverflow.com/a/682545/2745495. – Gino Mempin Dec 02 '20 at 12:24
  • I suggest re-reading the difference between instance and class methods, instance and class variables, and also how classes and instances are constructed and used in Python. – Gino Mempin Dec 02 '20 at 12:27

0 Answers0