Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

A recommendation for Python developers (OOP)

I learned that using something called a getter and a setter are better practice then directly accessing values from instances. I found that it is a little cleaner, although you write a bit more of code.

When you are inside of the class (where you use self), you don't need a getter or a setter. It's best practice to use getters and setters when you are accessing instance values from outside the class.

Here's an example.

Getter example

Worse:

class Person:
    def __init__(self, name):
        self.name = name

me = Person("Alex")
print(me.name)

Better:

class Person:
    def __init__(self, name):
        self.name = name
    def get_name(self):
        return self.name

me = Person("Alex")
print(me.get_name())

Setter example

Worse:

class Person:
    def __init__(self, name):
        self.name = name
    def get_name(self):
        return self.name

me = Person("Alex")
me.name = "Some other guy"

Better:

class Person:
    def __init__(self, name):
        self.name = name
    def get_name(self):
        return self.name
    def set_name(self, name):
        self.name = name

me = Person("Alex")
me.set_name("Some other guy")

What do you think, Kenneth Love, Steven Parker and Jennifer Nordell?

Steven Parker
Steven Parker
243,134 Points

:mailbox_with_mail: I got your message, but I see Kenneth already answered!

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

It's generally an anti-pattern to use getters and setters in Python. The fact that Python objects are so open (we don't have anything like private or protected variables/methods) should be a tip off for that. :D

But, no, don't write unnecessary getters or setters. If you have values that should be attributes but have to be calculated, you can make them a property which functions like a getter (and you can write setter methods for properties, too) but that's about it.

So, no, your "better" examples aren't better. Sorry to disappoint

Oh well... The professor at edX.org (Eric Grimson) taught in his class that it's best practice to use getters and setters in Python... False information? :/

Eric Grimson is a professor at MIT. I've also seen Getters and setters used a lot in other languages, for instance C#.

Add up all of these, I thought that getters and setters are good practice...

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Yes, completely false information. You gain nothing and increase the noise and messiness of your classes by following that pattern in Python. We're not that formal of a language, by design and by convention.

Those other languages have to use getters and setters because of how their classes are structured and how memory is stored/instances are defined. What's true for language A isn't always true for language B.

Look at the video here.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Let's see.

  • He's using a lowercase class name, that's not Pythonic.
  • Even though it's 2016 (and using 3.5 as I've seen elsewhere), he's inheriting from object, which is no longer needed.
  • He's using numer and denom when Python has no restriction on parameter or variable name length. Use descriptive names! Again, non-Pythonic.
  • He uses camelCase for method names, non-Pythonic.
  • He doesn't follow PEP 8, even nominally. Again, non-Pythonic.

Yes, these are small critiques but they show that he's less concerned with teaching good Python than with teaching computing concepts.

There is a highly recommended book called "Thinking In Python" that's free to read online. Do a search through it, "getter" and "setter" aren't even in the book. Ever notice that, when you have a regular expression Match object (which is a class, because everything in Python is a class), you've never had to do match.get_match_object() or float.get_denominator()?