Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Question about math.pi is mutable Date: Mon, 9 Nov 2015 02:11:26 +1100 Lines: 67 Message-ID: References: <87d1vlzy4p.fsf@elektro.pacujo.net> <878u69zxww.fsf@elektro.pacujo.net> <563eba85$0$1611$c3e8da3$5496439d@news.astraweb.com> <87ziyowy83.fsf@elektro.pacujo.net> <87mvuowvzo.fsf@elektro.pacujo.net> <87io5cwtja.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de 65EtLSfwXo6hKQg9TzEX9gOC3xWL3cdoyP6+a42xgODA== 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; 'resulting': 0.04; 'compiler': 0.05; 'subject:Question': 0.05; 'dst': 0.07; 'indices': 0.07; 'performs': 0.07; 'string;': 0.07; 'cc:addr :python-list': 0.09; 'dict': 0.09; 'differently.': 0.09; 'dst)': 0.09; 'globals': 0.09; 'lookup': 0.09; 'src': 0.09; 'tuple.': 0.09; 'example:': 0.10; 'assume': 0.11; 'def': 0.13; "'src'": 0.16; 'dictionary),': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hashes': 0.16; 'keyerror': 0.16; 'lookups': 0.16; 'nameerror': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'still,': 0.16; 'wrote:': 0.16; 'looked': 0.16; 'string': 0.17; 'attribute': 0.18; 'language': 0.19; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'otherwise,': 0.20; 'skip:" 40': 0.20; 'assuming': 0.22; 'function:': 0.22; 'sorry,': 0.22; 'am,': 0.23; 'bit': 0.23; 'seems': 0.23; 'replacing': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'module': 0.25; 'compare': 0.27; 'used,': 0.27; 'message- id:@mail.gmail.com': 0.27; "skip:' 10": 0.28; 'looks': 0.29; 'arguments,': 0.29; 'hash': 0.29; 'identifies': 0.29; 'name?': 0.29; 'str': 0.29; 'table,': 0.29; 'objects': 0.29; "i'm": 0.30; 'classes': 0.30; "we're": 0.30; 'normally': 0.30; 'skip:_ 10': 0.32; 'up.': 0.32; 'knows': 0.32; 'says': 0.32; 'table': 0.32; '[1]': 0.32; 'generally': 0.32; 'skip:. 10': 0.32; 'source': 0.33; 'instances': 0.33; 'gets': 0.35; 'received:google.com': 0.35; 'so,': 0.35; 'identified': 0.35; 'involving': 0.35; 'nov': 0.35; 'unicode': 0.35; 'something': 0.35; 'but': 0.36; 'instead': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'being': 0.37; 'method': 0.37; 'say': 0.37; 'received:209.85.213': 0.37; 'requirement': 0.37; 'things': 0.38; 'received:209': 0.38; 'names': 0.38; 'stuff': 0.38; 'anything': 0.38; 'means': 0.39; 'does': 0.39; 'still': 0.40; 'save': 0.60; 'your': 0.60; "you'll": 0.61; 'back': 0.62; 'is.': 0.63; 'different': 0.63; 'believe': 0.66; "they're": 0.66; 'legal': 0.66; 'talking': 0.67; 'levels': 0.70; 'anywhere.': 0.84; 'chrisa': 0.84; 'compare:': 0.84; 'to:none': 0.91; '8bit%:90': 0.93 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 :content-type:content-transfer-encoding; bh=wsXV1bfCVzOqWnA5Wm2YseQzdlo/KBToV7hYR3YD1To=; b=wG5aeqHlx43opzcYhLSpA2iTQM43SJEvWcr9HraSPYZwRQGYTPr/j44qsf5tGjuu72 tO7k7GxICFPHubljGhw8FijPrVLWBxvywNZuV3n9e++tWGUp5KuDzlWsgenvXvC344wa ldxqR5ZyULwZhubruQiDTEWjbO3jhB5IeV9bPdlIpFXgtpgYXz3/5GbN5li/yJjI2zpO WY5dqrSU2smcXCs2Mfu7l66ZJQKGUf53oquEp91pIz+pDL8s7x+46VR1eHN04JxQ4xVY 8aGK9Y27XAB5qiQ9Daq4vQVdQMwLpXvWURbyGTKNQaE0nkFdjT8ELp4/KLbcVZiLAMG3 48gw== X-Received: by 10.50.83.38 with SMTP id n6mr17990962igy.92.1446995486497; Sun, 08 Nov 2015 07:11:26 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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:98452 On Mon, Nov 9, 2015 at 12:42 AM, BartC wrote: > Sorry, you'll have to assume I'm very stupid. > > What exactly is being looked up, and in what? > > From what I can understand in your example: > > * You are calling shutil.copyfileobj with two arguments, which happen to = be > instances of classes Source and Dest. > > * Let's say these are known as src and dst inside .copyfileobj. > > * .copyfileobj then calls methods src.read() dst.write(). > > * Each of these method calls has the form A.B() which seems to me to be > little different from shutil.copyfileobj(). > > So to get back to what I was saying, does this lookup involving searching > for method B in object A, and if so, does it actually do a search by name= ? > > (Someone mentioned a precalculated hash, of "A", or of "src" or "dst", us= e > used, but still, it's looking something up in a table, and a hash table > lookup I believe still requires an string compare to check if you've got = the > right entry.) The lookups I'm talking about happen pretty much anywhere. Firstly, this function: def f(): shutil.copyfileobj(src, dst) looks up the global name 'shutil' (that's one dict lookup - module globals are a dictionary), then performs an attribute lookup on the resulting object with the name 'copyfileobj', then looks up the two global names 'src' and 'dst'. In fact[1], those strings can be found in f.__code__.co_names, and the byte-code identifies them by their indices in that tuple. These are str objects (note that this means they can be any legal Unicode string; "=D1=88=D1=83=D1=82=D0=B8=D0=BB.=D1=86=D0=BE=D0=BF=D1=8B=D1=84=D0= =B8=D0=BB=D0=B5=D0=BE=D0=B1=D0=B9(=D1=81=D1=80=D1=86, =D0=B4=D1=81=D1=82)" = is just as valid), and their hashes will normally be precomputed to save time. Now, things are a bit different with function-locals; the compiler always knows which names they are, and it can compile things differently. Compare: def f(src): shutil.copyfileobj(src, dst) Instead of looking up a global name 'src', this now looks up a local name. They're identified by slot positions, so the compiler simply knows that "src" is in slot #0, and instead of looking anything up, simply says "load the thing in slot #0". There can also be other levels of indirection, such as __getattr__ and __getattribute__, which affect how stuff gets looked up. But otherwise, name lookups generally involve poking a dict with a string and taking what comes back (possibly replacing KeyError with NameError or AttributeError as the case may be). ChrisA [1] Assuming we're using CPython, or something which uses the same byte-code; this is NOT a language requirement - none of this is.