Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'classes,': 0.05; 'method.': 0.07; '%s"': 0.09; 'caller': 0.09; 'here?': 0.09; 'if,': 0.09; 'inherited': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'jan': 0.12; 'callee': 0.16; 'letting': 0.16; 'logging,': 0.16; 'posted.': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subclass': 0.16; 'subject:log': 0.16; 'suite.': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'pieces': 0.19; 'examples': 0.20; 'not,': 0.20; 'header:User-Agent:1': 0.23; 'fairly': 0.24; 'define': 0.26; 'first,': 0.26; 'logging': 0.26; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'code': 0.31; 'getting': 0.31; "skip:' 10": 0.31; 'too.': 0.31; 'routine': 0.31; 'suites': 0.31; 'class': 0.32; 'skip:c 30': 0.32; 'skip:d 20': 0.34; 'common': 0.35; 'skip:s 30': 0.35; 'transaction': 0.35; 'test': 0.35; 'but': 0.35; 'subject:?': 0.36; 'so,': 0.37; 'two': 0.37; 'clear': 0.37; 'stopped': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'little': 0.38; 'expect': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'called': 0.40; 'read': 0.60; 'problems.': 0.60; 'skip:a 30': 0.61; 'received:173': 0.61; 'simple': 0.61; 'times': 0.62; 'different': 0.65; 'account': 0.65; 'production': 0.68; 'watching': 0.68; 'surprise': 0.74; 'grew': 0.84; 'received:fios.verizon.net': 0.84; 'working,': 0.84; 'convenience,': 0.91; 'good,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: to be pythonic: should caller or callee log? Date: Tue, 03 Sep 2013 17:01:22 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 87 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1378242095 news.xs4all.nl 15878 [2001:888:2000:d::a6]:53207 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:53584 On 9/3/2013 12:07 PM, Gildor Oronar wrote: > What would you choose? Do you put logging routine into caller or callee? > My intuitive answer is "callee does the logging, because that's where > action takes place", like this: > > class Account(): > def transaction(self, amount, target): > logging.info("Start transaction of %s to %s" % (amount, target)) > ... > > if '__name__' == '__main__' > .... > account.transaction(amount, target) > > > Simple and easy. Put logging code to the caller would be tedious - the > function is called about 20 times in different places. > > So far so good, but we grew up to have 10 different account classes: > > class AbsctractAccount(): > > class CreditAccount(AbstractAccount): > def transaction(self, amount, target): > logging.info("Start transaction of %s to %s" % (amount, target)) > ... > > class DebitAccount(AbstractAccount): > def transaction(self, amount, target): > logging.info("Start transaction of %s to %s" % (amount, target)) > ... > > class SomeOtherAccount(...) > .... > > Then letting the callee do the logging is also tedious now. > > What is the best practise here? > > If, for the convenience, we define transaction function in > AbstractAccount to just do the logging, and change inherited classes, > like this: > > class AbsctractAccount(): > def transaction(self, amount, target): > logging.info("Start transaction of %s to %s" % (amount, target)) > > class DebitAccount(AbstractAccount): > def transaction(self, amount, target): > super().transaction(amount,target) > .... > > Then we gethered logging code pieces all to one place, but it begets two > other problems. > > 1. It is a "surprise" that super().transaction must be called first, Not to me ;-). That is fairly standard in super examples I have seen posted. > not last, and that it does only the logging. If that is the only thing in common ... > 2. The code used to check whether "transaction" is implemented: > if hasAttr(DebitAccount, 'transaction'): # clear to read > has to be changed to check whether "transaction" is inherited: > if not DebitAccount.transaction is AbstractAccount.transaction: > > whose purpose is confusing, and whose cause is a little surprise too. I would expect that every account class has a transaction method. * If so, just call it, but assertIsNot(DebitAccount.transaction, AbstractAccount.transaction) for every subclass in your test suite. * If not, perhaps you need an abstract subclass TransAccount. Then use hasattr in production code and the isnot test in test code. > Also, the pythonic "boldly calling and watching for exception" stopped > working, because instead of getting AttributeError, we would get a line > of log and nothing more. This is what test suites are for. -- Terry Jan Reedy