CSSE1001 / MyPyTutor

Interactive tutorial application for Python3.
Other
7 stars 12 forks source link

Using Classes problem #157

Open pjritee opened 9 years ago

pjritee commented 9 years ago

If a student writes:

def print_friend_info(person):
    print(person._name)
    print(person._age)
    if self._friend is not None:
        print('Friends with', person._friend._name)

then there is an error "You need to print the person's name in print_friend_info"

The error should really be about not using the getters such as person.get_name()

sapi commented 9 years ago

Good point.

This question bugs me a bit anyway; I don't think getters are good practice, and most Python style guides I've seen advise against them.

Immutable attributes like this (strings, ints etc) should really just be directly exposed (eg, person.name) and later hidden behind @property if needed. Probably a bit complex for such an early course, but it would be nice to switch to using methods which actually do something (eg age(), but have the class take birth_year as an input so that there's work to do)

On 27 March 2015 at 10:09, Peter Robinson notifications@github.com wrote:

If a student writes:

def print_friend_info(person): print(person._name) print(person._age) if self._friend is not None: print('Friends with', person._friend._name)

then there is an error "You need to print the person's name in print_friend_info"

The error should really be about not using the getters such as person.get_name()

— Reply to this email directly or view it on GitHub https://github.com/CSSE1001/MyPyTutor/issues/157.

pjritee commented 9 years ago

I agree that, in principle, @property is the right way to go but, as you say, it's a bit advanced for an intro course.

sapi commented 9 years ago

Yeah. It would be nice to have a proper discussion about when things should be public/private (seeing as some data attributes, like age, arguably should be part of the public interface) but that's wishful thinking...

On 27 March 2015 at 10:22, Peter Robinson notifications@github.com wrote:

I agree that, in principle, @property https://github.com/property is the right way to go but, as you say, it's a bit advanced for an intro course.

— Reply to this email directly or view it on GitHub https://github.com/CSSE1001/MyPyTutor/issues/157#issuecomment-86768465.

jgat commented 9 years ago

I always dealt with it by reminding myself that many of these students will end up learning Java in csse2002, where everything is getter and setter methods and all attrs are private. So they might as well get used to it now.

In the past, whenever students would ask a question and/or express confusion about private attrs/methods or about the leading _ on attr names, I would often give a big answer around the necessity of distinguishing between public/private, and then end it with "we like to have all attributes private" (which isn't too bad of a lie).

Keep in mind that many students will interpret the _ as part of the "self.", and think that everything in init should always look like self._x = x (I have even seen students confused by things like self._y = x or self._z = []).

Anyway, that's beside the point of this issue (namely, an inaccurate error message in the analysis).

On Fri, 27 Mar 2015 11:28 Sean Purdon notifications@github.com wrote:

Yeah. It would be nice to have a proper discussion about when things should be public/private (seeing as some data attributes, like age, arguably should be part of the public interface) but that's wishful thinking...

On 27 March 2015 at 10:22, Peter Robinson notifications@github.com wrote:

I agree that, in principle, @property https://github.com/property is the right way to go but, as you say, it's a bit advanced for an intro course.

— Reply to this email directly or view it on GitHub <https://github.com/CSSE1001/MyPyTutor/issues/157#issuecomment-86768465 .

— Reply to this email directly or view it on GitHub https://github.com/CSSE1001/MyPyTutor/issues/157#issuecomment-86769517.