<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Atrribute error- Help please' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Atrribute error- Help please' posted on the 'Python' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Sat, 25 May 2013 16:19:04 -0700</pubDate>
    <lastBuildDate>Sat, 25 May 2013 16:19:04 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Atrribute error- Help please</title>
      <link>http://www.programmersheaven.com/mb/python/416154/416154/atrribute-error--help-please/</link>
      <description>Hello,&lt;br /&gt;
&lt;br /&gt;
I'm very new to programming and have just started learning python. I was wondering if someone could tell me what is wrong with this script:&lt;br /&gt;
&lt;br /&gt;
#! /usr/bin/python&lt;br /&gt;
class merchandise(object):&lt;br /&gt;
	def __init__(self,price): &lt;br /&gt;
		self.price = price&lt;br /&gt;
&lt;br /&gt;
	def buy(self,money):&lt;br /&gt;
		if money&amp;gt;price:&lt;br /&gt;
			print "felicitaciones lo has comprado"&lt;br /&gt;
&lt;br /&gt;
		else:&lt;br /&gt;
			print "lo siento eso no basta"&lt;br /&gt;
&lt;br /&gt;
	def sell(self,price):&lt;br /&gt;
		print "te puedo dar "+price&lt;br /&gt;
	        input= raw_input('--&amp;gt; ')&lt;br /&gt;
		&lt;br /&gt;
		if input=="yes":&lt;br /&gt;
			print "te doy "+price&lt;br /&gt;
		else:&lt;br /&gt;
			print "lo siento no puedo comprarlo hoy"&lt;br /&gt;
&lt;br /&gt;
	def get_price(self):&lt;br /&gt;
		return self.price&lt;br /&gt;
	price=property(get_price)&lt;br /&gt;
		&lt;br /&gt;
&lt;br /&gt;
book=merchandise(10)&lt;br /&gt;
cd=merchandise(20)&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
When I try to run it I get:&lt;br /&gt;
  File "test.py", line 27, in &amp;lt;module&amp;gt;&lt;br /&gt;
    book=merchandise(10)&lt;br /&gt;
  File "test.py", line 4, in __init__&lt;br /&gt;
    self.price = price&lt;br /&gt;
AttributeError: can't set attribute&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Like I said I'm pretty new to this and just playing around trying to learn a bit. Thanks in advance for any help.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/python/416154/416154/atrribute-error--help-please/</guid>
      <pubDate>Wed, 05 May 2010 03:39:32 -0700</pubDate>
      <category>Python</category>
    </item>
    <item>
      <title>Re: Atrribute error- Help please</title>
      <link>http://www.programmersheaven.com/mb/python/416154/416317/re-atrribute-error--help-please/#416317</link>
      <description>: #! /usr/bin/python&lt;br /&gt;
: class merchandise(object):&lt;br /&gt;
: 	def __init__(self,price): &lt;br /&gt;
: 		self.price = price&lt;br /&gt;
: &lt;br /&gt;
: 	def buy(self,money):&lt;br /&gt;
: 		if money&amp;gt;price:&lt;br /&gt;
: 			print "felicitaciones lo has comprado"&lt;br /&gt;
: &lt;br /&gt;
: 		else:&lt;br /&gt;
: 			print "lo siento eso no basta"&lt;br /&gt;
: &lt;br /&gt;
: 	def sell(self,price):&lt;br /&gt;
: 		print "te puedo dar "+price&lt;br /&gt;
: 	        input= raw_input('--&amp;gt; ')&lt;br /&gt;
: 		&lt;br /&gt;
: 		if input=="yes":&lt;br /&gt;
: 			print "te doy "+price&lt;br /&gt;
: 		else:&lt;br /&gt;
: 			print "lo siento no puedo comprarlo hoy"&lt;br /&gt;
: &lt;br /&gt;
: 	def get_price(self):&lt;br /&gt;
: 		return self.price&lt;br /&gt;
: 	price=property(get_price)&lt;br /&gt;
: 		&lt;br /&gt;
: &lt;br /&gt;
: book=merchandise(10)&lt;br /&gt;
: cd=merchandise(20)&lt;br /&gt;
: &lt;br /&gt;
: 	&lt;br /&gt;
: When I try to run it I get:&lt;br /&gt;
:   File "test.py", line 27, in &amp;lt;module&amp;gt;&lt;br /&gt;
:     book=merchandise(10)&lt;br /&gt;
:   File "test.py", line 4, in __init__&lt;br /&gt;
:     self.price = price&lt;br /&gt;
: AttributeError: can't set attribute&lt;br /&gt;
&lt;br /&gt;
According to &lt;a href="http://docs.python.org/library/functions.html"&gt;http://docs.python.org/library/functions.html&lt;/a&gt; in the Property section, when you put&lt;br /&gt;
price=property(get_price)&lt;br /&gt;
you created a read-only attribute of the class.  If you are looking for a fully functional sort of attribute, you can either define a set method and a delete method and pass those in to property, ie&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
def set_price(self, p):
  '''stuff'''
def del_price(self):
  '''stuff'''
price = property(get_price, set_price, del_price, '''doc string''')
&lt;/pre&gt;&lt;br /&gt;
Or, you could leave it at&lt;br /&gt;
self.price = price&lt;br /&gt;
like you have in your __init__ method.  The only reason I can think of why you would want to use property to define attributes would be because you need them to have special behavior.&lt;br /&gt;
Also, if you wanna stick with property, you need to name the final attribute something other than the first attribute.  If they are both called "price", you will get into a nasty recursion loop.  Calling the argument to __init__ "p" or "_price" would be a good solution.&lt;br /&gt;
&lt;br /&gt;
In your "buy" method, you reference price by itself, instead of self.price.  If price is an attribute of the class, and not of the instances of the class, that would be correct.  Since you (apparently) want price to be an instance attribute (different for each instance), you need to reference it using self.price.&lt;br /&gt;
&lt;br /&gt;
I hope that clears things up a bit.  If you start reading and get confused about "new style" and "old style" classes, you are in good company.  I always forget the difference.&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/python/416154/416317/re-atrribute-error--help-please/#416317</guid>
      <pubDate>Mon, 10 May 2010 16:21:15 -0700</pubDate>
      <category>Python</category>
    </item>
    <item>
      <title>Re: Atrribute error- Help please</title>
      <link>http://www.programmersheaven.com/mb/python/416154/416318/re-atrribute-error--help-please/#416318</link>
      <description>Hello,&lt;br /&gt;
&lt;br /&gt;
I went back over the sections on using self.  and property. This helped me clear up a few questions -- thanks a lot for the help :)</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/python/416154/416318/re-atrribute-error--help-please/#416318</guid>
      <pubDate>Mon, 10 May 2010 20:48:09 -0700</pubDate>
      <category>Python</category>
    </item>
  </channel>
</rss>