Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Gildor Oronar Newsgroups: comp.lang.python Subject: Re: to be pythonic: should caller or callee log? Date: Fri, 06 Sep 2013 23:05:59 +0800 Organization: A noiseless patient Spider Lines: 29 Message-ID: <5229EF57.3090709@mail-on.us> References: <148ea321-c4e3-457b-bcc8-865f480c80b6@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: mx05.eternal-september.org; posting-host="23f771972844b1e7dba51e195923b3d3"; logging-data="18261"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ygQxI0CTVeur+55VdjzI2fWjndwtnam4=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130803 Thunderbird/17.0.8 In-Reply-To: <148ea321-c4e3-457b-bcc8-865f480c80b6@googlegroups.com> Cancel-Lock: sha1:SIbYn+QLkzHsZ57bBl92f+xIfFw= Xref: csiph.com comp.lang.python:53788 El 04/09/13 20:14, Xaxa Urtiz escribió: > and what about something like that : > > > class AbsctractAccount(): > def transaction(self, amount, target): > logging.info("Start transaction of %s to %s" % (amount, target)) > self.DoTransaction(amount,target) > > def DoTransaction(self,amount,target): > pass # or raise notimplemented or do not implement this methods in the abstract class > ... > > class DebitAccount(AbstractAccount): > def DoTransaction(self, amount, target): > ... > > class SomeOtherAccount(...) > .... > like that you only have to write the logging function once. Thanks for the hint! This also work well, and has the advantage of being specific to this function -- I did use decorator as Ethan suggested, which works for most of the case, but there is function (other than transaction) needs specialized logging because the function doesn't return anything but changes a class variable -- using a special decorator for just one function is over-generalizing, and your method kicks in.