Python Forum
question about using setter, getter and _
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
question about using setter, getter and _
#5
__init_() uses the setter because the setter ensures that usernames are all lowercase. You could write the code like this:
    def __init__(self,username):
        self._username=username.lower()
But now you have two places in the code that process usernames. If you changed your code to make usernames uppercase you would have two places in the code you need to edit. Doing things in one place is always better than repeating the code in multiple places.

Next time really think things through before you post. I think you are so focused on the Python that you are not thinking about the design. Logically it is obvious that you would want the same rules to apply to username if it is the initial value (set in __init__()) or if it is changed at a later time (using property).
Reply


Messages In This Thread
RE: question about using setter, getter and _ - by deanhystad - Dec-28-2023, 09:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python class doesn't invoke setter during __init__, not sure if's not supposed to? mtldvl 2 3,414 Dec-30-2021, 04:01 PM
Last Post: mtldvl
  how to use getter as argument in function nanok66 3 3,352 May-13-2020, 09:15 AM
Last Post: nanok66
  Getter/Setter : get parent attribute, but no Getter/Setter in parent nboweb 2 3,041 May-11-2020, 07:22 PM
Last Post: nboweb
  Setter of class object maitreyaverma 1 2,477 Sep-28-2017, 06:15 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