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_na me)
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(sel f):
self.hunger += 1
self.boredom += 1
def __feed_time(sel f): # 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_tim e()
def eat(self, food = 4):
print "Brruppp. Thank you."
self.hunger -= food
self.__feed_tim e() # I wrote this
print self.feed # I wrote this
if self.hunger < 0:
self.hunger = 0
self.__pass_tim e()
def play(self, fun = 4):
print "Wheee!"
self.boredom -= fun
if self.boredom < 0:
self.boredom = 0
self.__pass_tim e()
##
def main():
crit_name = raw_input("What do you want to name your critter?: ")
crit = Critter(crit_na me)
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("Choi ce: ")
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.") 3 1834
On 24 Feb 2005 08:34:09 -0800, ne********@myre albox.com declaimed the
following in comp.lang.pytho n: 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_na me) 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.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG < wu******@dm.net | Bestiaria Support Staff < =============== =============== =============== =============== == < Home Page: <http://www.dm.net/~wulfraed/> < Overflow Page: <http://wlfraed.home.ne tcom.com/> <
On 24 Feb 2005 08:34:09 -0800, rumours say that ne********@myre albox.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_na me) 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(sel f): # 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...
I appreciate all of your help.
I learned a lot form your adovice.
Thanks.
Mr. Bieber, it worked fine. Thanks again. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 software giant such as Microsoft SQL
Server, Oracle, and Sybase?
Is PostgreSQL reliable enough to be used for high-end commercial
application? Thanks
|
by: titan0111 |
last post by:
#include<iostream>
#include<iomanip>
#include<cstring>
#include<fstream>
using namespace std;
class snowfall
{
private:
int ft;
|
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 example,
between a C/C++ programmers with a few weeks of experience
and a C/C++ programmer with years of experience.
You don't really need to understand the subtle details
or use the obscure features of either language
|
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 support IDEs are DreamWeaver 8 and
Zend PHP Studio.
DreamWeaver provides full support for Unicode. However, DreamWeaver is
a web editor rather than a PHP IDE. It only supports basic IntelliSense
(or code completion) and doesn't have anything...
|
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();
fopen("c:\\autoexec.bat","r")&&printf("success") || printf("error opeing
| |
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 C. wat type of
questions should be expected? Which part of C language do the staff
give more concern?
The interviewers have just mentioned that .. i will have interview on
C.
Also can anyone can help me with sites where i can go thru sample
|
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 believe MSFT should do to
improve C#, however.
I know that in the "Whidbey" release of VS.NET currently
|
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
|
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=
|
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 Record (i) is shown and modified, the change will
come to Record (i+1).
Can anyone provide suggestion?
thanks.
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |