Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone01.am1.xlned.com!bcyclone01.am1.xlned.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'essentially': 0.04; 'argument': 0.05; 'classes,': 0.05; 'tree': 0.05; 'attribute': 0.07; 'class,': 0.07; 'none,': 0.07; 'tom': 0.07; '__name__': 0.09; 'instance.': 0.09; 'moreover,': 0.09; 'name?': 0.09; 'sub': 0.09; 'terms,': 0.09; 'python': 0.11; 'def': 0.12; 'assume': 0.14; '%s,': 0.16; "'__main__':": 0.16; '(when': 0.16; '1):': 0.16; 'consequence,': 0.16; 'guys,': 0.16; 'hierarchy': 0.16; 'implies': 0.16; 'inheritance': 0.16; 'instance:': 0.16; 'ivan': 0.16; 'relevance': 0.16; 'skip:> 20': 0.16; 'stuff.': 0.16; 'subject:Classes': 0.16; 'subject:question.': 0.16; 'module': 0.19; 'translated': 0.19; 'version.': 0.19; 'written': 0.21; 'seems': 0.21; '(the': 0.22; '>>>': 0.22; 'bonus': 0.22; 'coding': 0.22; 'proposed': 0.22; 'form:': 0.24; 'instance,': 0.24; 'sends': 0.24; 'looks': 0.24; 'question': 0.24; 'equivalent': 0.26; 'this:': 0.26; 'pass': 0.26; 'code:': 0.26; 'defined': 0.27; 'skip:" 20': 0.27; '----': 0.29; 'code': 0.31; "skip:' 10": 0.31; 'along.': 0.31; 'embedding': 0.31; 'invoke': 0.31; 'object.': 0.31; 'symbolic': 0.31; 'class': 0.32; 'run': 0.32; 'skip:_ 10': 0.34; 'classes': 0.35; 'common': 0.35; 'case,': 0.35; 'usual': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'method': 0.36; 'charset:us-ascii': 0.36; 'thanks': 0.36; "i'll": 0.36; 'should': 0.36; 'turn': 0.37; 'two': 0.37; 'message-id:@gmail.com': 0.38; 'manager': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'skip:. 10': 0.39; 'subject:" ': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'skip:p 20': 0.39; 'called': 0.40; 'according': 0.40; 'how': 0.40; 'name': 0.63; 'such': 0.63; 'received:79': 0.64; 'provide': 0.64; 'more': 0.64; 'effectively': 0.66; 'here': 0.66; 'direct': 0.67; 'percent': 0.68; 'subject': 0.69; 'person.': 0.69; 'hand': 0.80; 'book:': 0.84; "class's": 0.84; 'leverages': 0.84; 'needed:': 0.84; 'percent,': 0.84; 'sue': 0.84; 'hand,': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:mime-version:content-type :content-transfer-encoding:thread-index:content-language; bh=+JCJO0AD+5wj7dFJ0CiX1VKgtFU44KI25aOLs2QnRus=; b=NLspB0D1vaWF6lp62zPSMgz3kGkE/uMzv5YpWriPo35ZIvyDU4YL8LMByFwc+niDf7 B3nQjlSiGGW2VQt2RD+yrzn8ASXnGgxQ8sGcI5LAKcTCilDB7wJBdychH7W590dBri1x flqynSTLmj8K1qeKN/XelV2/6SkGV9UUfHBsbxaQgXt6odlQgRqget8j5EYOaX2eVfI0 13mwerV5GZAh5DYBNEzW+XiBs99vazCm58je9KJfKCE9lN0hxAp+N45TagAYPlyYCrp4 ymPbGeDst6TvlLE1XDK4TWRhf2KaednLj67ymvalAydRptJTwAl590mbSSYb36/2puQs Ajmg== X-Received: by 10.194.187.164 with SMTP id ft4mr71601529wjc.76.1418827008887; Wed, 17 Dec 2014 06:36:48 -0800 (PST) From: "Ivan Evstegneev" To: Subject: Classes - "delegation" question. Date: Wed, 17 Dec 2014 16:35:36 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AdAZ+QxMXTMTUb/2QzW4Ieye6AerSQ== Content-Language: en-us 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: 217 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418827010 news.xs4all.nl 2951 [2001:888:2000:d::a6]:35787 X-Complaints-To: abuse@xs4all.nl X-Received-Body-CRC: 461634442 X-Received-Bytes: 9812 Xref: csiph.com comp.lang.python:82597 Hello guys, I have a question about "delegation" coding pattern(I'm working with Python 3.4). In order to describe my question , I'll provide particular example: Let assume I have two classes written in module named person.py: Case 1: class Person: def __init__(self, name, job = None, pay =0): self.name = name self.job = job self.pay = pay def lastName(self): return self.name.split()[-1] def giveRaise(self, percent): self.pay = int(sel.pay * (1 + percent)) def __repr__(self): return '[Person: %s, %s]' % (self.name, self.pay) class Manager(Person): def giveRaise(self, percent, bonus = .10): Person.giveRaise(self, percent + bonus) Case 2: class Person: ....the same... class Manager: def __init__(self, name, pay): self.person = Person(name, 'mgr', pay) def giveRaise(self, percent, bonus = .10): self.person.giveRaise(percent + bonus) def __getattr__(self, attr): return getattr(self.person, attr) def __repr__(self): return str(self.person) I also used some test code proposed by the book: if __name__ == '__main__': bob = Person('Bob Smith') sue = Person('Sue Jones', job='dev', pay=100000) print(bob) print(sue) print(bob.lastName(), sue.lastName()) sue.giveRaise(.10) print(sue) tom = Manager('Tom Jones', 50000) # Job name not needed: tom.giveRaise(.10) print(tom.lastName()) print(tom) As I can understand from the above code : When code looks like in Case 1, it actually works with standard "inheritance" model. (with sub and super-class definitions). Also according to the book: *************************************************************** This code leverages the fact that a class's method can always be called either through an instance (the usual way, where Python sends the instance to the self argument automatically) or through the class (the less common scheme, where you must pass the instance manually). In more symbolic terms, recall that a normal method call of this form: instance.method(args...) is automatically translated by Python into this equivalent form: class.method(instance, args...) *************************************************************** Furthermore: *************************************************************** Python provides it automatically only for calls made through an instance. For calls through the class name, you need to send an instance to self yourself; for code inside a method like giveRaise, self already is the subject of the call, and hence the instance to pass along. Calling through the class directly effectively subverts inheritance and kicks the call higher up the class tree to run a specific version. **************************************************************** What is considered as a "call" in this context? Is it a code inside class method? For instance(relates to the "Case 1"): def giveRaise(self, percent, bonus = .10): Person.giveRaise(self, percent + bonus) The "call" will be : Person.giveRaise(self, percent + bonus) ? In such a case, when I create an instance: >>> tom = Manager('Tom Jones', 50000) and then invoke giveRaise method >>> tom.giveRaise(.10) I essentially say this: "class.method(instance, args...) " Where class is "Person" and method is "giveRaise" and instance is "tom"? Moreover, because of how it defined (a method giveRaise), I make a "call" through the class name? What is the "call" through instance? Is it this one(according to Case 1): class Person: ....... def giveRaise(self, percent): self.pay = int(sel.pay * (1 + percent)) ....... and then: >>> bob = Person('Bob Smith', 'dev', 30000) >>> bob.giveRaise(.10) < ---- Is this call through instance? Questions regard to "Case 2" example: class Person: ....the same... class Manager: def __init__(self, name, pay): self.person = Person(name, 'mgr', pay) <------- Initialization of Manager's attribute named 'self.person" and embedding a Person object. def giveRaise(self, percent, bonus = .10): < ------ Defenition of Manager's method named "giveRaise" and according to the book: self.person.giveRaise(percent + bonus) "Intercept and delegate" .............. My question here is substantially the same: On the one hand we embedding "Person" into "Manager" class, On the other hand, when I write this code: >>> tom = Manager('Tom Jones', 5000) I create an instance of Manager that have an attribute named: tom.person, which in turn is a class named Person. And when typing: >>>tom.giveRaise(.10) I do kinda same thing. For my assumption I say: Person.giveRaise( percent + bonus) (when self.person actually implies "Person") But why we don't use "self" argument in --> self.person.giveRaise(percent + bonus)? So it should look like --> self.person.giveRaise(self, percent + bonus)? Is it because of embedding "Person" into "Manager" so there is no hierarchy considerations, and by embedding(compositing) classes, we place them at the same level? And as it seems to me, there is no relevance for " inheritance" stuff. As a direct consequence, when I type: >>> tom.giveRaise(.10) I made a "call" through instance, and there is no need for "self"? Thanks a lot in advance, Ivan