Dec-27-2021, 03:28 AM
(This post was last modified: Dec-28-2021, 04:48 AM by deanhystad.)
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.
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.