Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Classes problem #157

Open
pjritee opened this issue Mar 27, 2015 · 4 comments
Open

Using Classes problem #157

pjritee opened this issue Mar 27, 2015 · 4 comments
Labels

Comments

@pjritee
Copy link
Contributor

pjritee commented Mar 27, 2015

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()

@pjritee pjritee added the bug label Mar 27, 2015
@sapi
Copy link
Contributor

sapi commented Mar 27, 2015

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 [email protected] 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
#157.

@pjritee
Copy link
Contributor Author

pjritee commented Mar 27, 2015

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
Copy link
Contributor

sapi commented Mar 27, 2015

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 [email protected] 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
#157 (comment).

@jgat
Copy link
Contributor

jgat commented Mar 27, 2015

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 [email protected] 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 [email protected]
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
<#157 (comment)
.


Reply to this email directly or view it on GitHub
#157 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants