on-internet
← Community

one function as a class and also instance method

Stack Overflow ·

This could be a so called XY problem, so let me start with what I want: ed = Edit.add(1).add(2).add(3) print(ed.op) # [1,2,3] Following is what I tried and basically I got working code, but I'm not sure if I did it right or if there are other options. The first add(1) is a class method call and the following add(2) and add(3) are instance method calls. I started here: class Edit: def __init__(self): self.op = [] @classmethod def add(cls, x): self = cls() return self.add_im(x) def add_im(self, x): # im = instance method self.op.append(x) return self ed = Edit.add(1).add_im(2).add_im(3) print(ed

Comments

Sign in or sign up to comment and upvote.

No comments yet.