3

I want to create a class myCalc that has a constructor, with get and set methods and uses a property. I understand the get and set methods but am having trouble with putting all of the pieces together so it performs what I want it to do. What I have so far is this:

class myCalc(object):

    def __init__(self):
            self._ =name

        def (self):
            """Get the answer doubled."""
            return self._plus

        def (self):
            """Get the answer squared."""
            return self._times

I'd like to get as far as having the program respond with properties that double and square the number in the object. Any suggestions would be helpful.

martineau
  • 119,623
  • 25
  • 170
  • 301
fractal21
  • 31
  • 1
  • 1
  • 5

3 Answers3

5

A property object has getter, setter, and deleter methods You should use decorators as follows:

class C(object):
    def __init__(self):
        self._x = None

    @property
    def x(self):
        """I'm the 'x' property."""
        return self._x

    @x.setter
    def x(self, value):
        self._x = value

    @x.deleter
    def x(self):
        del self._x

If you want it readonly, just use @property and remove the two others

Serjik
  • 10,543
  • 8
  • 61
  • 70
  • Sorry for the delay! Appreciate the answers so far. The reason why I want a get and set method is to retrieve and set the value of a property named “name,” that reflects the object’s name. – fractal21 Sep 10 '13 at 03:11
1

A simple object that has doubled and squared properties

class myCalc(object):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return str(self.value)
    def dbler(self):
        return self.value*2
    doubled = property(dbler)
    squared = property(lambda self: self.value**2)
    # equivalent with the decorator
    @property
    def cubed(self):
        return self.value**3

if __name__ == '__main__':
    x = myCalc(10)
    print x # 10
    print x.doubled  # 20
    print x.squared  # 100
    print x.cubed # 1000

Adapted from http://docs.python.org/2/howto/descriptor.html#properties

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • If I wanted to adapt this to allow a user to change the name by using the get and set properties, would it need the raw input code to refer back to the get/set? – fractal21 Sep 10 '13 at 03:25
0

class myCalc(object):

def_init_(self, value)
self.value=value

def_init_(self, name)
print "Welcome to myCalc!"

self._name=name

def get_name(self):
        return self._name

def set_name(self, new_name):
    if new_name=="":
        print "You mut enter a name."
    else:
        self._name=new_name
        print "The name has been changed."

def_str_(self)
return str(self.value)

def dbler(self):
    return self.value*2

doubled=property(dbler)
squared=property(lambda self: self.value**2)

name=property(get_name, set_name)

def talk(self):
    print "\nWelcome, this is," self.name

if name=='main': x= myCalc(5) print x print x.doubled print x.squared

mainline

calc=myMath("Calculator") calc.talk()

print "\nmyCalc's name is:", print calc.name

print "\nChanging myCalc's name." calc.name=""

calc.talk()

raw_input("\n\nPress the enter key to exit."

Here is what I am looking to do, however I keep coming across errors. I want to be able to change the object name AND number.

fractal21
  • 31
  • 1
  • 1
  • 5