Atrribute error- Help please

Hello,

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:

#! /usr/bin/python
class merchandise(object):
def __init__(self,price):
self.price = price

def buy(self,money):
if money>price:
print "felicitaciones lo has comprado"

else:
print "lo siento eso no basta"

def sell(self,price):
print "te puedo dar "+price
input= raw_input('--> ')

if input=="yes":
print "te doy "+price
else:
print "lo siento no puedo comprarlo hoy"

def get_price(self):
return self.price
price=property(get_price)


book=merchandise(10)
cd=merchandise(20)


When I try to run it I get:
File "test.py", line 27, in
book=merchandise(10)
File "test.py", line 4, in __init__
self.price = price
AttributeError: can't set attribute


Like I said I'm pretty new to this and just playing around trying to learn a bit. Thanks in advance for any help.







Comments

  • : #! /usr/bin/python
    : class merchandise(object):
    : def __init__(self,price):
    : self.price = price
    :
    : def buy(self,money):
    : if money>price:
    : print "felicitaciones lo has comprado"
    :
    : else:
    : print "lo siento eso no basta"
    :
    : def sell(self,price):
    : print "te puedo dar "+price
    : input= raw_input('--> ')
    :
    : if input=="yes":
    : print "te doy "+price
    : else:
    : print "lo siento no puedo comprarlo hoy"
    :
    : def get_price(self):
    : return self.price
    : price=property(get_price)
    :
    :
    : book=merchandise(10)
    : cd=merchandise(20)
    :
    :
    : When I try to run it I get:
    : File "test.py", line 27, in
    : book=merchandise(10)
    : File "test.py", line 4, in __init__
    : self.price = price
    : AttributeError: can't set attribute

    According to http://docs.python.org/library/functions.html in the Property section, when you put
    price=property(get_price)
    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
    [code]
    def set_price(self, p):
    '''stuff'''
    def del_price(self):
    '''stuff'''
    price = property(get_price, set_price, del_price, '''doc string''')
    [/code]
    Or, you could leave it at
    self.price = price
    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.
    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.

    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.

    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.
  • Hello,

    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 :)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories