Python Forum
I need help understanding a program structure using classes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need help understanding a program structure using classes
#18
I strongly disagree that setters and getters are better than directly accessing attributes. Use a setter when setting an attribute requires additional processing. For example, if your objects are entries in a database you probably want to modify the database when you set the value of an attribute. This is easy to do with a setter method and/or using the property decorator. However, if your attribute is just a variable and setting the variable doesn't require any additional processing, I would not use a setter.

The thinking that setters and getters make better code can be traced back to poorly designed languages like C++ and all their derivatives and the languages they influenced. Here you often needed setters and getters, or using setters and getters provides some level of protection by limiting how you could access the attributes. In Python a setter/getter provides no projection at all and usually just adds extra code that has to be maintained.

Never, ever use "from module import *". The reason for modules is primarily to generate name spaces, not reduce the amount of code in a file. When you create a namespace you are protection your variable and class names from colliding with variables or class names in code written by other people. Tkinter, a popular Python GUI package, has classes with names like Label and Button. These are nice names. If I make my own type of button I would probably like to call it Button. Since the tkinter Button is tkinter.Button I can do that and not worry about my program confusing my buttons with the tkinter buttons. When you "from module inport *" you strip the namespace protection and all the imported attributes.
BashBedlam likes this post
Reply


Messages In This Thread
RE: I need help understanding a program structure using classes - by deanhystad - Dec-27-2021, 03:28 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PLEASE DELETE_Python OOP program structure TeddyT 0 383 Apr-26-2025, 10:00 AM
Last Post: TeddyT
  Understanding Python classes PythonNewbee 3 2,280 Nov-10-2022, 11:07 PM
Last Post: deanhystad
  Understanding Python super() for classes OmegaRed94 1 2,517 Jun-09-2021, 09:02 AM
Last Post: buran
  Understanding program blocks newbieAuggie2019 2 2,859 Oct-02-2019, 06:22 PM
Last Post: newbieAuggie2019
  help with understanding a program prompt drasil 5 4,197 Feb-14-2019, 05:54 PM
Last Post: ichabod801
  Help, not understanding how classes work... Peter_EU 1 3,187 Jan-20-2018, 06:07 PM
Last Post: wavic
  Using classes? Can I just use classes to structure code? muteboy 5 6,558 Nov-01-2017, 04:20 PM
Last Post: metulburr
  I need help understanding how to use and run this program! Thanks in advance! tc1chosen 6 6,491 Sep-01-2017, 01:56 PM
Last Post: tc1chosen

Forum Jump:

User Panel Messages

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