[Tutor] Class access rules

Phil phillor9 at gmail.com
Sun Mar 6 19:33:59 EST 2022


I've imported the following class into several wxpython projects and now 
into a tkinter project and now I'm wondering if I should modify the 
class by adding setter and getter methods instead of directly accessing 
the instance attributes. I know that Python will warn me that I 
shouldn't access these attributes if I prefix them with an underscore. 
My question is does it really matter or is simply just a method to warn 
programmers who work in a group to not access those attributes?

class Led:
     def __init__(self, pos, state=False, size=10):

         self.pos = pos # position is a tuple (x, y)
         self.state = state # True is on, False is off
         self.size = size
         self.onColour = 'red'
         self.offColour = 'black'

     def toggle(self):
         self.state = not self.state

I'm thinking of adding a blink method (I'm not sure what use it would 
be). My initial thoughts seem to show that the method would be specific 
to tkinter rather than generic. I don't have a method to draw the LED 
either and instead use either GUI to draw a circle based on the 
position, state, size and colour of the LED class attributes. I'm 
thinking that I should do the same with the blink method or should I 
have specific methods (draw circle and blink) for wxpython and tkinter? 
I don't think so, but I'm not sure.

  --
Regards,
Phil



More information about the Tutor mailing list