how to use property

neutrinman at myrealbox.com neutrinman at myrealbox.com
Fri Feb 25 05:25:45 EST 2005


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




More information about the Python-list mailing list