473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

what is wrong?

I cannot find out why the following code generates the error:
Traceback (most recent call last):
File "D:/a/Utilities/python/ptyhon22/test.py", line 97, in ?
main()
File "D:/a/Utilities/python/ptyhon22/test.py", line 60, in main
crit = Critter(crit_name)
File "D:/a/Utilities/python/ptyhon22/test.py", line 8, in __init__
self.feed = feed # I wrote this
AttributeError: can't set attribute

I add some codes to a program on a book. The lines that have "I wrote
this" comment is the added codes. Could anyone tell me what is worng
here?

--------------
##
class Critter(object):
"""A virtual pet"""
def __init__(self, name, hunger = 0, boredom = 0, feed = 0):
self.name = name
self.hunger = hunger
self.boredom = boredom
self.feed = feed # I wrote this

def __pass_time(self):
self.hunger += 1
self.boredom += 1

def __feed_time(self): # I worte this
self.feed += 1 # I wote this

def __get_feed(self): # I wrote this
return self.feed # I worte this

feed = property(__get_feed) # I wrote this

def __get_mood(self):
unhappiness = self.hunger + self.boredom
if unhappiness < 5:
mood = "happy"
elif 5 <= unhappiness <= 10:
mood = "okay"
elif 11 <= unhappiness <= 15:
mood = "frustrated"
else:
mood = "mad"
return mood

mood = property(__get_mood)

def talk(self):
print "I'm", self.name, "and I feel", self.mood, "now.\n"
self.__pass_time()

def eat(self, food = 4):
print "Brruppp. Thank you."
self.hunger -= food
self.__feed_time() # I wrote this
print self.feed # I wrote this
if self.hunger < 0:
self.hunger = 0
self.__pass_time()

def play(self, fun = 4):
print "Wheee!"
self.boredom -= fun
if self.boredom < 0:
self.boredom = 0
self.__pass_time()

##

def main():
crit_name = raw_input("What do you want to name your critter?: ")
crit = Critter(crit_name)

choice = None
while choice != "0":
print \
"""
Critter Caretaker

0 - Quit
1 - Listen to your critter
2 - Feed your critter
3 - Play with your critter
"""

choice = raw_input("Choice: ")
print

# exit
if choice == "0":
print "Good-bye."

# listen to your critter
elif choice == "1":
crit.talk()

# feed your critter
elif choice == "2":
crit.eat()

# play with your critter
elif choice == "3":
crit.play()

# some unknown choice
else:
print "\nSorry, but", choice, "isn't a valid choice."

main()
("\n\nPress the enter key to exit.")

Jul 18 '05 #1
3 1810
On 24 Feb 2005 08:34:09 -0800, ne********@myrealbox.com declaimed the
following in comp.lang.python:
I cannot find out why the following code generates the error:
Traceback (most recent call last):
File "D:/a/Utilities/python/ptyhon22/test.py", line 97, in ?
main()
File "D:/a/Utilities/python/ptyhon22/test.py", line 60, in main
crit = Critter(crit_name)
File "D:/a/Utilities/python/ptyhon22/test.py", line 8, in __init__
self.feed = feed # I wrote this
AttributeError: can't set attribute
Off-hand, you defined "feed" to be a property, and created a get
method for it... but no set method.

I could be all wet, since I haven't used properties in Python
(haven't written anything big enough, meant for other users, to need the
protection).

The other problem is that it is ambiguous if

self.feed

refers to the data storage space for the value, or the PROPERTY itself

feed = property().

Try changing all the

self.feed

references to something like

self.myFeed

and report back if that works (or doesn't)...

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #2
On 24 Feb 2005 08:34:09 -0800, rumours say that ne********@myrealbox.com might
have written:
I cannot find out why the following code generates the error:
Traceback (most recent call last):
File "D:/a/Utilities/python/ptyhon22/test.py", line 97, in ?
main()
File "D:/a/Utilities/python/ptyhon22/test.py", line 60, in main
crit = Critter(crit_name)
File "D:/a/Utilities/python/ptyhon22/test.py", line 8, in __init__
self.feed = feed # I wrote this
AttributeError: can't set attribute I add some codes to a program on a book. The lines that have "I wrote
this" comment is the added codes. Could anyone tell me what is worng
here?
[snip]
class Critter(object):
This fails as you said: self.feed = feed # I wrote this
[snip]

And this would fail: def __feed_time(self): # I worte this
self.feed += 1 # I wote this
Because of this: feed = property(__get_feed) # I wrote this

which specifies only a 'get' method for the property.

[snip]

feed is a property, and there is not 'set' method for it, so it's behaving as
read-only. Search for 'property' in your python docs.
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 18 '05 #3
I appreciate all of your help.
I learned a lot form your adovice.
Thanks.

Mr. Bieber, it worked fine. Thanks again.

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
5
by: titan0111 | last post by:
#include<iostream> #include<iomanip> #include<cstring> #include<fstream> using namespace std; class snowfall { private: int ft;
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
28
by: Madhur | last post by:
Hello what about this nice way to open a file in single line rather than using if and else. #include<stdio.h> void main() { FILE *nd; clrscr();...
56
by: Cherrish Vaidiyan | last post by:
Frinds, Hope everyone is doing fine.i feel pointers to be the most toughest part in C. i have just completed learning pointers & arrays related portions. I need to attend technical interview on...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
3
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.