So I have this piece of code originally,
class Foo:
def __init__(self, val:int):
self.val = val
def next(self) -> Foo:
"""
Returns:
Foo: Foo with value 1 bigger than the current
"""
return Foo(self.val+1)
which, the next() method is to generate another Foo instance based on the current instance.
It works fine so far, but then I have read about static method and class method in python, and it seemed that I might should using a class method for next()
, as it is a factory method (please point out if I got the idea wrong!)
What would be the most elegant way to implement the above?
Consider:
class Foo:
index = 0
def __init__(self):
self.val = Foo.index
Foo.index += 1
Now, you just have to do:
zero = Foo()
one = Foo()
And if you want to force a starting value:
Foo.index = 99
n99 = Foo()
n100 = Foo()