-1

I am new and trying property() in python. I saw an example and tried to execute that but I am receiving an error. I tried the code below:

class proper(object):
      def __init__(self):
          self.x = 4   
      def setx(self,val):
          self.x = val
      def getx(self):
          return self.x
      def delx(self):
          del self.x

      p = property(setx,getx,delx,'i am doc')

pr = proper()
pr.setx(7)
print pr.getx()

error:

Traceback (most recent call last):
  File "./test3.py", line 148, in <module>
    pr.p=5
TypeError: getx() takes exactly 1 argument (2 given)

3 Answers 3

1

I have no idea what line 148 is - but it doesn't refer to the code you posted.

The whole point of using property is that you can define methods which make a data item look like an attribute.

With your class definition as posted you can now do :

pr = proper()
pr.x = 7 # No need to call pr.setx
print pr.x # no need to call pr.sety

This is a very basic and redundant use of properties.

But say you define setx as :

def setx( self, val):
    if val < 0:
        raise ValueError("'x' cannot be negative")
     self.x = val

You now have a special attribute x which can never be negative. Note I have not been able to test this, as I don't have access to a Python implementation.

3
  • ok, if we do call x with object then it is simple and no use of property(). Actually i wanted to know the use of property() Apr 19, 2014 at 17:11
  • The use of property is that you can define your setx, getx methods to do special things - for instance: limit the values that the attribute can be set to, calculate a value for your attribute on the fly rather than storing it. In your simple example though there is no benefit to using property. Apr 19, 2014 at 17:50
  • I have updated my answer with an example of a useful setx definition Apr 19, 2014 at 18:04
1

The error you are getting is because you have created your p property with its arguments in the wrong order.

Try:

p = property(getx, setx, delx, 'i am doc') # getter before setter!

The error was happening because getx was being called when setx should have been. Because they take different numbers of arguments, this was breaking things.

Note that a more elegant way of setting up a property is to use decorators as you define the accessor methods:

@property
def p(self):         # formerly getx
    'i am doc'
    return self.x

@p.setter
def p(self, value):  # formerly setx
    self.x = value

@p.deleter
def p(self):         # formerly delx
    del self.x
0

You are misunderstanding how properties are meant to be accessed.

Adapted from the Python documentation for property: If then pr is an instance of proper, pr.x will invoke getx, pr.x = setx will invoke the setter and del pr.x the deleter, delx.

Therefore, you aren't supposed to call it as pr.setx(7).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.