Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Encapsulation in Python Date: Sun, 13 Mar 2016 08:10:35 +1100 Lines: 56 Message-ID: References: <56E17985.7060002@benmezger.nl> <6984dafc-fffd-4d46-a173-421bb5c142d2@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 1V+pqDJ+CFWfkIOiVexQmw8me5ySr2PJwOyms+vBnBiw== 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; 'else:': 0.03; 'received:209.85.223': 0.03; 'elif': 0.04; 'subject:Python': 0.05; 'cc:addr:python-list': 0.09; 'accelerator': 0.09; 'happen.': 0.09; 'imported': 0.09; 'importerror': 0.09; 'tracing': 0.09; 'python': 0.10; 'skip:# 20': 0.13; 'def': 0.13; 'importing': 0.15; '2016': 0.16; '__init__.py': 0.16; '_socket': 0.16; 'behaviour.': 0.16; 'contrived': 0.16; 'foo()': 0.16; 'foo():': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'pulling': 0.16; 'reason).': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'otherwise,': 0.20; 'suppose': 0.22; 'am,': 0.23; 'this:': 0.23; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'module': 0.25; "i've": 0.25; "doesn't": 0.26; 'message-id:@mail.gmail.com': 0.27; 'correct': 0.28; 'this.': 0.28; 'looks': 0.29; '13,': 0.29; 'block,': 0.29; 'once,': 0.29; 'symbols': 0.29; 'raise': 0.29; 'foo': 0.33; 'jump': 0.33; "i'll": 0.33; 'received:google.com': 0.35; 'could': 0.35; 'exist': 0.35; 'step': 0.36; 'but': 0.36; 'received:209.85': 0.36; 'modules': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'johnson': 0.37; 'things': 0.38; 'received:209': 0.38; 'stuff': 0.38; 'where': 0.40; 'ever': 0.60; 'more': 0.63; 'necessarily': 0.63; 'mar': 0.65; 'detail.': 0.66; 'therefore': 0.67; 'levels': 0.70; 'chrisa': 0.84; 'modx': 0.84; 'to:none': 0.91; 'rick': 0.93; 'imagine': 0.96; 'subject:skip:E 10': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=G0XmmOu4TO+fBMzxOngafQ54NONdT9JWW7CcqRKGYTc=; b=rypEE95TQnNO/a/AF3PEWLUDNEUlUz1C+yFgmotrNG5X+or7vJZlXNkLx9tjsZ4MaL m2SGbKHB1R/tT663Wn807zaSautIPpEWvL5aptdsEfZe7wW7f/wNvgF8j3tktbHiy5d/ OZLXUrUItSC1zG2rI2hvDD4u+Tr2nk0YCgQQELtqIGA3d4EybvNHLXAexc8TehsqIO2X /vII5/3Ls3NvvHL0aQG0YNrUKdTVdufZgj70PJPYvypASjFqLw2Crz2BP7ZsXVH6IQo/ jOyf327mXimF3qViJYgtiqNL2BcBGpoogGlZCE/JQ+vPsNIhRrJYA6cGUcqu7fqcp4bm 3a8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=G0XmmOu4TO+fBMzxOngafQ54NONdT9JWW7CcqRKGYTc=; b=jn+jTM7wUnR7T7+yoy6mpjor460HDubKgscO5PqNiAk2SJE0NQsXEta7vfqOAMXRhK etwWe8KRCn3Germ+XBXsTRHgpQHCVPB0Fao3garPm4svRp4p5mjwL39eHxIYTips5YVF 7CrLrJXrYnvRyXUnghj9zR+53r3r7q6OoljouLp0L69zMcPUy5ChkUGLAVJ37XzezQrk 93pe0gxGiF/Vr+axQ4zToPqQZ1fDrjy9tp7rI/qjNtZ2ucTf+/Y0oLa4ebESZuRPqa7Q pFvCxrs+rtnKrxYSHBVwkGOk/XeR8wDau/aYUKbxpLCliyFYY24iWcs2d+/4MTpJu0d6 AG5w== X-Gm-Message-State: AD7BkJKPbT4iP7zDd6oQEgUn7llCsrvEvS1WtQhtBL5xiqy7gJk8G3JZYHndnSC+bdzyLrAmj8qUMNkNyWvSxA== X-Received: by 10.107.138.75 with SMTP id m72mr17077767iod.31.1457817035513; Sat, 12 Mar 2016 13:10:35 -0800 (PST) In-Reply-To: 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:104726 On Sun, Mar 13, 2016 at 3:49 AM, Rick Johnson wrote: > Imagine this scenario: > > #################### > # currentModule.py # > #################### > from modX import foo > > def bar(): > return foo() > > ########### > # modX.py # > ########### > from modY import foo > > ########### > # modY.py # > ########### > from modZ import foo > > ########### > # modZ.py # > ########### > def foo(): > return 'O:-)' > > I'll admit this is a highly contrived example, but it is not > invalid in anyway, and could therefore exist in reality. I've never seen public symbols deliberately being imported through multiple levels like this. The most I've seen is __init__.py pulling stuff in from one of its modules (eg "from .constants import *"), or a Python module importing from its accelerator ("from _socket import *"). In theory, I suppose you could have both at once, but that's the most you'd ever get, and the three would be very tightly coupled. Otherwise, this simply doesn't happen. Also, if currentModule.py is pulling foo from modX, then modZ.py is an implementation detail. You don't necessarily want to go straight there; tracing the chain is more likely to be the correct behaviour. Suppose modX.py actually looks like this: if 'posix' in some_magic: import posixY as modY elif 'nt' in some_magic: import ntY as modY else: raise ImportError The correct way to find out where modX.foo comes from is to look at this block, not to jump right through it. Law of Demeter - you take things one step at a time (unless there's a good reason). ChrisA