how to use property
Kartic
removethis.kartic.krishnamurthy at gmail.com
Fri Feb 25 08:45:09 EST 2005
neutrinman at myrealbox.com said the following on 2/25/2005 5:25 AM:
> My question is how should I use "property" which wraps up
> __get_channel() and __set_channel()in the following program.
> I tried the program that written below, and it worked. Then I tried:
> channel = property(__get_channel,__set_channel) as in comment 1, 2,
> and 3,
>
> but it generates the error:
> Traceback (most recent call last):
> File "C:/WINDOWS/desktop/test.py", line 41, in -toplevel-
> main()
> File "C:/WINDOWS/desktpo/test.py", line 37, in main
> tv.change_channel(choice)
> File "C:/WINDOWS/desktop/test.py", line 27, in change_channel
> self.channel(choice)
> TypeError: 'int' object is not callable
>
> I am now studying class, so want to use property.
> This program simulates changing channel number 1 through 10 on TV.
> How should I use property here?
>
> def change_channel(self, choice):
> self.__set_channel(choice)
> print self.__get_channel()
>
> #comment2: self.channel(choice)
You see comment#2... You say self.channel(choice). self.channel is an
int object that you are trying to call - channel(choice) - and that is
what your exception states when it says 'int object is not callable'.
You probably meant self.channel = choice
Also, there is a problem in your posted version. You have not defined
volume before usage. So I added it to __init__(..,.., volume=120) to
make it work.
Cheers,
-Kartic
More information about the Python-list
mailing list