how to use property

N

neutrinman

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?

---------------------------------------
##simulating a TV

##defining class
class TV(object):
def __init__(self, channel = 1):
self.__channel = channel
self.__volume = volume
print "the default channel is", self.__channel

def __get_channel(self):
return self.__channel

def __set_channel(self, choice):
if choice == "+":
self.__channel += 1
elif choice == "-":
self.__channel -= 1
if self.__channel == 0:
self.__channel = 10
elif self.__channel == 11:
self.__channel = 1

#comment1: channel = property(__get_channel, __set_channel)

def change_channel(self, choice):
self.__set_channel(choice)
print self.__get_channel()

#comment2: self.channel(choice)
#comment3: print "channel: ", self.channel


##main part
def main():
tv = TV()
choice = " "
while choice:
choice = raw_input("choice (+ / -): ")
tv.change_channel(choice)


##starting program
main()
 
K

Kartic

(e-mail address removed) 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top