Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getters and setters
#8
(May-15-2017, 06:18 AM)Ofnuts Wrote:
(May-15-2017, 03:09 AM)Skaperen Wrote: my new class has set_opts() and get_opts().  set_opts() works from named options.  get_opts() returns a dict with the options.  an additional feature: set_opts() returns a dict of unrecognized options.  this new class has 8 options (there are not setters or getters for individual options because of so many).

The problem with getters/setters is not code verbosity. It is that they allow inconsistent states. If you rename some "Marmaduke Skaperen" to "Alexandre-Benoit Ofnuts" with setters on first name and last name, your object can temporarily be set to "Marmaduke Ofnuts" or "Alexandre-Benoit Skaperen".

but ... should i change this part of my code?

    def get_opts(self):
        return {
            'gutter':  self.gutter,
            'width':   self.width,
            'height':  self.height,
            'height2': self.height2,
            'top':     self.top,
            'left':    self.left,
            'right':   self.right,
            'bottom':  self.bottom,
        }
    def set_opts(self,**opts):
        for o in option_names_all:
            if o in opts:
                v = opts.pop(o)
                if v != None:
                    setattr(self,'_'+o,v)
        return opts
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
getters and setters - by Skaperen - May-13-2017, 01:06 AM
RE: getters and setters - by micseydel - May-13-2017, 01:16 AM
RE: getters and setters - by Mekire - May-13-2017, 03:14 AM
RE: getters and setters - by Skaperen - May-13-2017, 04:33 AM
RE: getters and setters - by Ofnuts - May-14-2017, 03:30 PM
RE: getters and setters - by Skaperen - May-15-2017, 03:09 AM
RE: getters and setters - by Ofnuts - May-15-2017, 06:18 AM
RE: getters and setters - by Skaperen - May-15-2017, 07:36 AM
RE: getters and setters - by Ofnuts - May-15-2017, 09:12 PM
RE: getters and setters - by Skaperen - May-16-2017, 04:27 AM
RE: getters and setters - by Ofnuts - May-16-2017, 07:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Can property getters and setters have additional arguments? pjfarley3 2 3,099 Oct-30-2020, 12:17 AM
Last Post: pjfarley3
  rework of a little python-script - extending getters and setters apollo 7 4,344 Nov-04-2018, 01:37 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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