On 10/31/2014 12:31 PM, Seymore4Head wrote:

In this class, we will follow the practice of accessing the contents
of objects using methods known as getters and setters. While not
required by Python, this practice encourages the user of the class to
manipulates class objects solely via class methods. The advantage of
following this practice is that the implementer of the class
definition (often someone other than the user of the class) may
restructure the organization of the data fields associated with the
object while avoiding the need to rewrite code that uses the class.


Written by somebody who doesn't understand a fundamental point about Python. It's just not true that the user would have to rewrite code if the implementer change the members.

The Python way of writing getters and setters (if/when you get to that stage of a Python class) is through the property decorator or its equivalent.

class Pet():
    def __init__(self, ....):
        self.internal details set here

    @property()
    def age(self):
        return some_complicated_expression

And the user of the class fetches this by using

mypet = Pet(....)
hisage = mypet.age

No stupid parentheses needed. When the class is first written, no work is needed. When it's updated to multiply the internal age by 7, just write the function, and decorate it to look like a regular attribute.

More complicated things can be done. But the point is that the user should just get the age, by accessing the age attribute, and if the implementation needs to change, it can change.

The time to make a class complicated, is when it needs to be. And the user should not have to pay the price for "just in case."

--
DaveA
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to