0

Class intSet

I'm trying to understand the following code from the MIT python class. When i create an object of the class intSet as follows i run into some trouble with one of the attributes.

s=intSet()

and try

s.vals()

I get the following error

  Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    s.vals()
TypeError: 'list' object is not callable

why is this? I've been told i shouldn't try s.vals directly because of data hiding, when i try s.vals the correct list is returned but i get an error when trying s.vals(). Can someone explain this error? I'm new to both OOP and python so i apologise for my poor questioning. Any help is greatly appreciated.

8
  • Why the parentheses? Commented Jul 17, 2017 at 17:37
  • Please post all code here, as text. Commented Jul 17, 2017 at 17:38
  • What do you expect that adding the parentheses does? Parentheses try to use the attribute like a function, i.e., it tries to call the attribute like a function. As the error states, s.vals is a list, which isn't callable. Commented Jul 17, 2017 at 17:39
  • 2
    BTW, while that is a great course, it unfortunately does not stick to typical python conventions. If you want an attribute to be "private", pre-pend an underscore, self._vals = []. This is privacy by convention, no one is actually prevented from accessing your attribute. But people who use your code base know "this is an implementation detail and not a guarantee, don't use this, and if you do, know you can't rely on this behavior". Also, Python generally is written using "snake_case" instead of "camelCase". Commented Jul 17, 2017 at 17:44
  • 1
    @cᴏʟᴅsᴘᴇᴇᴅ heh, I find it particularly pleasant because snake -> python :) Commented Jul 17, 2017 at 17:47

3 Answers 3

2

vals is not a method it is an attribute so you can't call it. In python the parentheses indicate you are calling a method. So just do s.vals

Sign up to request clarification or add additional context in comments.

1 Comment

Great to hear. If you could up vote and/or accept that would be awesome :)
1

When you use s.vals() it tries to call function 'vals' through variable 's' whereas, when you use s.vals it doesnt look for the function just returns the value stored in s.vals.

Comments

0

You are trying to call vals that is a list. You are doing something like this [](), that is not possible.

What you should do in OOP way is to declare getter function like that:

class intSet(object):
    def __init__(self):
        self.__vals = [] # kinda private class instance
        # .... other code


    @property
    def vals(self):
        return self.__vals

intset = intSet()
intset.vals # is that list

More info about private members in python and about properties

10 Comments

do not use double-underscore name-mangling. This is not the purpose of it, use a single underscore.
I know that :-) The single underscore does not create the masking of name, in fact. If you put single _, it seems like protected member in terms of OOP languages. The true way of private member is to make it double-underscored.
@juanpa.arrivillaga Is right. That's not what double-underscore name-mangling is suppose to be used for. In short, it's used to avoid naming-conflicts among child classes. Along with that, there really is no reason for self.vals to be private. have getters and setters simply out of convention in Python is generally frowned upon.
No, this is shoe-horning Java/C++ ideas about access modifiers into a language that simply does not support them. What you are saying in the Python world with double-underscore name-mangling is "let this name be used by subclasses without shadowing the parent attribute". Python is not Java. Don't write Python like Java.
@ChristianDean exactly! The whole point of having properties is that you don't write them unless you need them. If you write a property that simply does return self._attribute, then you likely don't need it in the first place. Just use self.attribute. If you need to change the way self.attribute is accessed/set at some later point, then use the property. That is the beauty of properties, we don't have to write stupid getter/setter boilerplate. See this tutorial.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.