Aug-01-2020, 08:50 AM
(This post was last modified: Aug-01-2020, 08:56 AM by Gribouillis.)
Python_User Wrote:Inside class objects, should I use the SELF argument only in the __init__ def?Normally, you need to use the
self
argument in every method definition except when they are class methods or static methods (defined with the @classmethod and @staticmethod decorators). The self argument represents the object instance when the method is called. Actually, you could use another word than 'self' for this argument, but 'self' is traditional and that's what other python programmers expect.The variables
v, i
in this __init__()
method are the counter's initial value and increment. I don't think these are very good names because the __init__
function is the main public interface of the class. A user will call for example co = CountFromBy(5, 2)
, so the meaning of these variables should be as explicit as possible. I would have used 'start' and 'step' because these names are used in the prototype of the well known range()
function and every Python programmer will understand them immediatelyclass CountFromBy: def __init__(self, start=0, step=1): self.val = start self.incr = stepThe annotations such as
: int
or -> str
are not required in Python, although some people may consider that it makes a better documented code. It is a trend in today's Python to clutter the code with annotations. Personally, I don't want to sacrifice the 'informal' side of the Python language that makes it so flexible, so I prefer to avoid type annotations.