Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #47936

Re: How to pass instance into decorator function

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'argument': 0.05; 'sufficient': 0.05; '*args,': 0.09; 'arguments': 0.09; 'inherited': 0.09; 'percentage': 0.09; 'raises': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:instance': 0.09; 'subject:into': 0.09; 'wrapper': 0.09; 'subject:How': 0.10; 'def': 0.12; "'''": 0.16; '**kwargs)': 0.16; '**kwargs):': 0.16; '0.2': 0.16; '@property': 0.16; 'arg': 0.16; 'calculates': 0.16; 'components.': 0.16; 'given)': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'shortcut': 0.16; 'subject:pass': 0.16; 'typeerror:': 0.16; 'wraps': 0.16; 'component': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'import': 0.22; 'header:User-Agent:1': 0.23; 'body,': 0.24; 'class.': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'code': 0.31; 'calculated': 0.31; 'class': 0.32; 'skip:c 30': 0.32; 'whom': 0.33; 'implemented': 0.33; 'skip:d 20': 0.34; 'basic': 0.35; 'classes': 0.35; 'but': 0.35; 'executing': 0.36; 'implement': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'remove': 0.60; 'company': 0.60; 'till': 0.61; 'first': 0.61; 'total': 0.65; 'occur': 0.65; 'sample': 0.67; 'exceed': 0.68; 'amount.': 0.84; 'body:': 0.84; 'calculations': 0.84; 'fyi': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: How to pass instance into decorator function
Date Thu, 13 Jun 2013 13:26:07 +0200
Organization None
References <e84043fc-9c4e-46a9-b7a1-8c136a4fbdf6@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084b0b0.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3179.1371122752.3114.python-list@python.org> (permalink)
Lines 83
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1371122752 news.xs4all.nl 15869 [2001:888:2000:d::a6]:49725
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:47936

Show key headers only | View raw


Jayakrishnan Damodaran wrote:

> I have a class which calculates some salary allowances. Instead of a blind
> calculation I need to check some conditions before I can return the
> calculated amount. Like if all the allowances calculated till now plus the
> one in progress must not exceed the total salary(this may occur since
> these allowances are a percentage of basic pay). Below is a sample code
> FYI
> 
> class CompanySalaryBreakUpRule(object):
>     '''Class to calculate the various salary components.
>     This class is inherited by individual company classes whom implement
>     salary calculations. Various component calculations are implemented as
>     @property and hence be accessed as class properties.
> 
>     '''
>     grossPay = 0.0
>     remainingAmount = 0.0
> 
>     def checkValid(self, func):
>         from functools import wraps
> 
>         @wraps(func)
>         def wrapper(self, *args, **kwargs):
>             allowanceToCheck = func(self, *args, **kwargs)
>             if allowanceToCheck > self.remainingAmount:
>                 allowanceToCheck = self.remainingAmount
>             else:
>                 self.remainingAmount = self.remainingAmount -
>                 allowanceToCheck
>             return allowanceToCheck
>         return wrapper
>     
>     @property
>     @checkValid
>     def dearnessAllowance(self):
>         return self.basic * 0.2 # we have a function that calculates basic
>         pay
> 
> But executing this raises an exception
> @checkValid
> TypeError: checkValid() takes exactly 2 arguments (0 given)

Remember that

@checkValid
def dearnessAllowance(self): 
...

is a shortcut for

def dearnessAllowance(self): 
...
dearnessAllowance = checkValid(dearnessAllowance)

The function call happens in the class body, so checkValid() is still normal 
function that has knowlegde neither about the class nor (as the self arg 
implies) about an instance of that class. 
While it would be sufficient to remove its first argument

> class CompanySalaryBreakUpRule(object):
      ...
>     def checkValid(func):
         ...

>     @property
>     @checkValid
>     def dearnessAllowance(self):
         ...

I recommend that you also move it out of the class body:

  def checkValid(func):
         ...

> class CompanySalaryBreakUpRule(object):
      ...
>     @property
>     @checkValid
>     def dearnessAllowance(self):
         ...

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

How to pass instance into decorator function Jayakrishnan Damodaran <jakyjk05@gmail.com> - 2013-06-13 03:40 -0700
  Re: How to pass instance into decorator function Peter Otten <__peter__@web.de> - 2013-06-13 13:26 +0200

csiph-web