Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.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; 'python,': 0.01; 'attributes': 0.05; 'distinct': 0.05; 'subject:Python': 0.06; 'override': 0.07; 'pep': 0.07; '__name__': 0.09; 'builtin': 0.09; 'default)': 0.09; 'executed': 0.09; 'hooks': 0.09; 'namespace': 0.09; 'classes,': 0.13; 'stored': 0.13; '302': 0.16; 'capability,': 0.16; 'discarded': 0.16; 'instantiate': 0.16; 'metaclass': 0.16; 'metaclasses': 0.16; 'observations': 0.16; 'syntax': 0.16; 'to:name:python-list': 0.18; 'mechanism': 0.21; 'modification': 0.23; 'welcome.': 0.23; 'code': 0.25; 'statement': 0.25; 'function': 0.27; 'reflect': 0.28; 'import': 0.28; 'message- id:@mail.gmail.com': 0.29; 'skip:( 20': 0.29; 'definition': 0.30; '(f)': 0.30; 'modules,': 0.30; 'objects': 0.32; 'comment': 0.32; 'to:addr:python-list': 0.33; 'object': 0.35; 'post': 0.36; '(by': 0.37; 'partial': 0.37; 'skip:i 30': 0.37; 'but': 0.37; 'role': 0.38; 'some': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'to:addr:python.org': 0.39; 'more': 0.60; 'unique': 0.62; 'here': 0.65; 'special': 0.67; "'class'": 0.84; '(based': 0.84; 'locals': 0.84; 'mechanics': 0.84; 'partially': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; bh=qY9OZuKNHSAIaXFBkTZGW3BwaqSbbjQ7BtCMj4hZyDE=; b=TpyCHnzQzylRADJ4GGMtJviWQqENJ8tnRa0rZqp/7VyI78FayOfCzcEN/eYOKRYhNz KAWZm6qc7dm43LCL0yY/1lpnI4lGtVeYCQRwv2g5vK21/rHWjXMC3kq2b/c4Ev50VrJV tpos3hSuWjrTNJjaLxKXbIjbXiMZYxNr0g9tk= MIME-Version: 1.0 Date: Fri, 5 Aug 2011 01:20:24 -0600 Subject: Observations on the three pillars of Python execution From: Eric Snow To: python-list Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 86 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312528828 news.xs4all.nl 23860 [2001:888:2000:d::a6]:42018 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10891 In Python, three types of objects have special syntax and mechanics for their instantiation, during which a code object is generated: modules, classes, and functions. Each has its own role to play and the differences between them mostly reflect that. Here are some observations (based on default behavior): Object creation relative to code object creation: (M) before (C) after (F) after Object creation relative to code object execution: (M) before (C) after (F) before Object available during code object execution: (M) no (C) no (F) no Code object destiny: (M) executed once at definition time, then discarded (C) executed once at definition time, then discarded (F) not executed at definition time, stored on the function object for execution with each __call__() invocation. Execution locals is available during code object execution: (M) as globals() (C) as locals() (F) as locals() Object namespace is execution locals of code object: (M) yes (C) more or less (F) completely distinct Unique syntax: (M) 'import' statement (C) 'class' statement (F) 'def' statement Mechanism triggered by respective syntax: (M) import machinery (import.c/importlib) (C) internal, but partially exposed via metaclasses and the __build_class__ builtin (F) internal, nothing exposed Override capability: (M) complete capability through builtin __import__ and PEP 302 import hooks (C) partial capability, before code object execution through metaclass __prepare__() and after through __build_class__() and metaclass __call__(), __new__(), and __init__() (F) no capability Post instantiation modification capability: (M) yes (C) yes (F) mostly (some attributes are read-only) Mechanism to instantiate outside of respective unique syntax: (M) imp.new_module(), types.ModuleType(), type()() (C) type() (F) types.FunctionType(), type(f)() Type extensibility: (M) Not relative to 'import' syntax (by default) (C) Complete (F) No Name available during definition execution: (M) as __name__ (C) only through metaclass __prepare__() (F) through inspect.currentframe().f_code.co_name Name available on object as __name__: (M) yes (C) yes (F) yes Corrections, additions, and comment are welcome. -eric