dynamically inserting function into an object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • michael

    #1

    dynamically inserting function into an object

    Hi,

    below is a snipplet that could be seen as a part of a spreadsheet with
    getter and setter properties and a way how to dynamically insert
    function to be used when setting the value of a "cell" instance


    import new
    import inspect

    class Cell (object):

    def __init__ (self, initialvalue = 0):
    self._func = None
    self.__value = initialvalue

    def setvalue (self, newvalue):
    if self._func:
    self.__value = self._recalcula te (newvalue)
    else:
    self.__value = newvalue

    def getvalue (self):
    return self.__value

    def _recalculate (self, value):

    ret_value = self._func (value)

    return ret_value

    def delvalue (self):
    del self.__value


    value = property(getval ue, setvalue, delvalue, "I'm the 'value'
    property.")

    def curry(self, func, *args):
    self._func = new.function(fu nc.func_code, func.func_globa ls,
    argdefs=args)

    func = property(curry, "I'm the 'func' property.")

    def func (value, firstcell, secondcell):
    return value + firstcell.value + secondcell.valu e

    cell0 = Cell (10)
    cell1 = Cell (20)

    curriedcell = Cell (100)

    print "uncurried initial %d " % (curriedcell.va lue)

    curriedcell.val ue = 60

    print "uncurried set %d " % (curriedcell.va lue)

    curriedcell.cur ry (func, cell0, cell1)
    curriedcell.val ue = 62

    print "curried set %d " % (curriedcell.va lue)



    Is there a better way to do this or am I totally on the wrong way ?

    Regards

    Michael
  • John Roth

    #2
    Re: dynamically inserting function into an object

    If what you want is to insert a method into an
    instance, look at new.instancemet hod.

    John Roth

    "michael" <michael.bieren feld@web.de> wrote in message
    news:f11c8440.0 501130808.6c50a 62b@posting.goo gle.com...[color=blue]
    > Hi,
    >
    > below is a snipplet that could be seen as a part of a spreadsheet with
    > getter and setter properties and a way how to dynamically insert
    > function to be used when setting the value of a "cell" instance
    >
    >
    > import new
    > import inspect
    >
    > class Cell (object):
    >
    > def __init__ (self, initialvalue = 0):
    > self._func = None
    > self.__value = initialvalue
    >
    > def setvalue (self, newvalue):
    > if self._func:
    > self.__value = self._recalcula te (newvalue)
    > else:
    > self.__value = newvalue
    >
    > def getvalue (self):
    > return self.__value
    >
    > def _recalculate (self, value):
    >
    > ret_value = self._func (value)
    >
    > return ret_value
    >
    > def delvalue (self):
    > del self.__value
    >
    >
    > value = property(getval ue, setvalue, delvalue, "I'm the 'value'
    > property.")
    >
    > def curry(self, func, *args):
    > self._func = new.function(fu nc.func_code, func.func_globa ls,
    > argdefs=args)
    >
    > func = property(curry, "I'm the 'func' property.")
    >
    > def func (value, firstcell, secondcell):
    > return value + firstcell.value + secondcell.valu e
    >
    > cell0 = Cell (10)
    > cell1 = Cell (20)
    >
    > curriedcell = Cell (100)
    >
    > print "uncurried initial %d " % (curriedcell.va lue)
    >
    > curriedcell.val ue = 60
    >
    > print "uncurried set %d " % (curriedcell.va lue)
    >
    > curriedcell.cur ry (func, cell0, cell1)
    > curriedcell.val ue = 62
    >
    > print "curried set %d " % (curriedcell.va lue)
    >
    >
    >
    > Is there a better way to do this or am I totally on the wrong way ?
    >
    > Regards
    >
    > Michael[/color]

    Comment

    • hanz

      #3
      Re: dynamically inserting function into an object

      # class Cell(object):
      # def __init__(self, initialvalue = 0):
      # self._func = lambda x: x
      # self.__value = initialvalue
      #
      # def setvalue (self, newvalue):
      # self.__value = self._func(newv alue)
      #
      # def getvalue (self):
      # return self.__value
      #
      # def delvalue (self):
      # del self.__value
      #
      # value = property(getval ue, setvalue, delvalue, "I'm the 'value'
      property.")
      #
      # def curry(self, func, *args):
      # self._func = lambda x: func(x, *args)

      Comment

      • michael.bierenfeld@web.de

        #4
        Re: dynamically inserting function into an object

        *damn* it :-) python rocks.
        thx

        michael .oO (resetting c/c++/java crap collected over the years)

        Comment

        Working...