property data-descriptor - how to pass additional constants to theget/set method

Discussion in 'Python' started by petr.jakes.tpc@gmail.com, Feb 25, 2008.

  1. Guest

    Hi,
    I would like to pass additional constants to the property data-
    descriptor, so it is not necessary to write many methods which differ
    just by constant defined in the method body.
    Till now I can do this:

    class MyClass(object):

    def _getValue(self, myConstant):
    print 'myConstant:', myConstant
    return myConstant+10

    def _setValue(self, inputValue, myConstant):
    print 'inputValue:', inputValue, 'myConstant:', myConstant
    return inputValue*myConstant

    fistAttribte = property(fget = lambda self:
    self._getValue(myConstant=0),
    fset = lambda self, inputValue, myConstant=0:
    self._setValue(inputValue, myConstant))

    secAttribute = property(fget = lambda self:
    self._getValue(myConstant=1),
    fset = lambda self, inputValue, myConstant=1:
    self._setValue(inputValue, myConstant))

    It works, but it does not look very nice to me.
    Is there some different/nicer/more pythonic way how to achieve above
    mentioned?

    regards

    Petr Jakes
     
    , Feb 25, 2008
    #1
    1. Advertising

  2. Re: property data-descriptor - how to pass additional constants tothe get/set method

    En Mon, 25 Feb 2008 18:50:20 -0200, <> escribió:

    > Hi,
    > I would like to pass additional constants to the property data-
    > descriptor, so it is not necessary to write many methods which differ
    > just by constant defined in the method body.
    > Till now I can do this:
    >
    > class MyClass(object):
    >
    > def _getValue(self, myConstant):
    > print 'myConstant:', myConstant
    > return myConstant+10
    >
    > def _setValue(self, inputValue, myConstant):
    > print 'inputValue:', inputValue, 'myConstant:', myConstant
    > return inputValue*myConstant
    >
    > fistAttribte = property(fget = lambda self:
    > self._getValue(myConstant=0),
    > fset = lambda self, inputValue, myConstant=0:
    > self._setValue(inputValue, myConstant))
    >
    > secAttribute = property(fget = lambda self:
    > self._getValue(myConstant=1),
    > fset = lambda self, inputValue, myConstant=1:
    > self._setValue(inputValue, myConstant))
    >
    > It works, but it does not look very nice to me.
    > Is there some different/nicer/more pythonic way how to achieve above
    > mentioned?


    Try using functools.partial

    --
    Gabriel Genellina
     
    Gabriel Genellina, Feb 26, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    2,613
  2. kj.kjn
    Replies:
    2
    Views:
    332
    Pierre Schnizer
    Jul 3, 2003
  3. gamehack
    Replies:
    2
    Views:
    507
    James Stroud
    Jul 18, 2007
  4. Zhang, Zhenhua
    Replies:
    0
    Views:
    566
    Zhang, Zhenhua
    May 12, 2010
  5. Matt

    Set column size with additional style sheet setting

    Matt, Jul 1, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    151
Loading...

Share This Page