Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python classes
#2
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 immediately
class CountFromBy:
    def __init__(self, start=0, step=1):
        self.val = start
        self.incr = step
The 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.
Reply


Messages In This Thread
Python classes - by Python_User - Aug-01-2020, 08:13 AM
RE: Python classes - by Gribouillis - Aug-01-2020, 08:50 AM
RE: Python classes - by Python_User - Aug-01-2020, 09:46 AM
RE: Python classes - by snippsat - Aug-01-2020, 10:27 AM
RE: Python classes - by Python_User - Aug-02-2020, 08:04 AM
RE: Python classes - by Gribouillis - Aug-02-2020, 09:32 AM
RE: Python classes - by Python_User - Aug-02-2020, 09:41 AM
RE: Python classes - by snippsat - Aug-02-2020, 10:08 AM
RE: Python classes - by deanhystad - Aug-02-2020, 10:50 AM
RE: Python classes - by Python_User - Aug-02-2020, 03:02 PM
RE: Python classes - by deanhystad - Aug-02-2020, 03:49 PM
RE: Python classes - by Python_User - Aug-02-2020, 05:53 PM
RE: Python classes - by deanhystad - Aug-02-2020, 08:18 PM
RE: Python classes - by Python_User - Aug-03-2020, 06:38 PM
RE: Python classes - by bowlofred - Aug-03-2020, 08:13 PM
RE: Python classes - by Python_User - Aug-04-2020, 06:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Classes rob101 4 595 Feb-05-2024, 06:51 PM
Last Post: rob101
  Understanding Python classes PythonNewbee 3 1,250 Nov-10-2022, 11:07 PM
Last Post: deanhystad
Sad Python classes PythonNewbee 4 1,108 Nov-09-2022, 01:19 PM
Last Post: deanhystad
  Inheritance vs Instantiation for Python classes mr_byte31 7 3,019 Oct-14-2021, 12:58 PM
Last Post: mr_byte31
  Understanding Python super() for classes OmegaRed94 1 1,884 Jun-09-2021, 09:02 AM
Last Post: buran
  Python Classes leodavinci1990 1 2,123 Nov-27-2019, 07:25 AM
Last Post: buran
  Using classes? Can I just use classes to structure code? muteboy 5 5,156 Nov-01-2017, 04:20 PM
Last Post: metulburr
  use of classes in python Lux 2 3,572 Aug-19-2017, 12:29 PM
Last Post: hbknjr
  python classes prsdll6 14 7,551 Aug-17-2017, 07:26 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020