Suppose I am creating a class representing and object in a KV-datastore. Creating the constructor is pretty straightforward...
class KVObject:
def __init__(self, key, value, add=True):
self.key = key
self.value = value
if add == True:
kv_create(key, value)
Now, let's say I want to retrieve a KV-object... and construct a class instance from it. I figure I'll add this class method onto my DataBaseObject class.
@classmethod
def get_kv_object(cls, key):
value = kv_get(key)
cls.__init__(key, value, add=False)
... but when calling init from the class method (cls.__init__), Python asks for an argument for self!
Any ideas? Many thanks! I know this is a bit of a simple example, but it definitely applies to some more complex, interesting situations!
Edit: Thanks for the responses. I learned something helpful while researching this, which is that object.__new__ calls __init__ on an object when calling MyClass(...), effectively providing an uninitialized instance of the class to MyClass.init with the args called from within MyClass. Likewise, it makes sense to call cls(...), as suggested in your answers. Thank you.