Python noob can't get class method to work

主宰稳场 提交于 2019-12-02 05:54:53

The first argument to a classmethod is the class itself. Try

@classmethod
def save_from_row(cls, row):
    c = cls()
    # ...
    return c

or

@staticmethod
def save_from_row(row):
    c = Customer()
    # ...
    return c

The classmethod variant will enable to create subclasses of Customer with the same factory function.

Instead of the staticmethod variant, I'd usually use module-level functions.

You want:

@classmethod
def save_from_row(cls, row):

Class methods get the method's class as the first argument.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!