Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: What is the common technique used to cross-reference in module's method? Date: Thu, 17 Mar 2016 12:09:50 +0100 Organization: None Lines: 57 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de KvefGLGI/HY1R/Fhfxg51AeWIUBhKUjuSnNPNaLOwn8w== 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; '-----------': 0.04; 'position,': 0.04; 'context': 0.05; 'assigning': 0.09; 'confuse': 0.09; 'instance.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:method': 0.09; 'subject:module': 0.09; 'def': 0.13; 'explicitly': 0.15; '(say': 0.16; "module's": 0.16; 'names?': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:reference': 0.16; 'wrote:': 0.16; 'memory': 0.17; 'attribute': 0.18; 'creates': 0.18; '(the': 0.22; 'pass': 0.22; 'import': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:m 30': 0.27; 'function': 0.28; 'idea': 0.28; 'methods.': 0.29; 'value)': 0.29; 'you?': 0.30; "i'd": 0.31; 'skip:s 30': 0.31; 'returned': 0.32; 'class': 0.33; 'common': 0.33; 'skip:- 10': 0.34; 'this?': 0.34; 'item': 0.35; 'but': 0.36; 'there': 0.36; 'modules': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'doing': 0.38; 'self': 0.38; 'skip:p 20': 0.38; 'why': 0.39; 'does': 0.39; 'subject:the': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'save': 0.60; 'avoid': 0.61; 'provide': 0.61; 'note:': 0.66; 'natural': 0.67; 'saw': 0.77; 'subject:common': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd9d39.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105084 jfong@ms4.hinet.net wrote: > There are two modules (say model.py and piece.py) which has methods need > to refer to each other module's methods. I saw in a book using the way > below, by assigning one (the Model) object to an attribute of the other > (the Piece) bject. ------------- > ##model.py > import piece > ... > class Model(dict): > ... > def all_occupied_positions(self): > ... > > def reset_to_initial_locations(self): > self.clear() > for position, value in START_PIECES_POSITION.items(): > self[position] = piece.create_piece(value) > self[position].model = self I'd pass self to the factory self[position] = piece.create_piece(self, value) > ... > > ##piece.py > ... > def create_piece(value): # Note: it's a function > ... > return eval(...) # the returned object is a Piece object and would avoid the eval(). > class Piece(): > ... > def moves_available(self): > model = self.model > ... > if item not in model.all_occupied_positions(): > ... > ... > ----------- > Is it a common way of doing this? Yes, but it creates a reference cycle; if you want to avoid that for philosophical reasons or to save memory when there are many children (here: pieces) you have to provide the context (the model in this case) explicitly def moves_available(self, model): ... > Why the author use the same module name "model" for those attribute and > local names? Is it a good idea or bad? The name "model" and "piece" look natural for both the module and the instance. Does it confuse you? then it's bad ;)