Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'parameters': 0.04; 'explicit': 0.07; 'level,': 0.07; 'sys': 0.07; 'classes.': 0.09; 'definition,': 0.09; 'root,': 0.09; 'subject:module': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'itself.': 0.14; '.py': 0.16; 'accesses': 0.16; 'arg):': 0.16; 'code),': 0.16; 'properties,': 0.16; 'semantics': 0.16; 'subject:type': 0.16; 'files.': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'bit': 0.19; 'module': 0.19; 'basically': 0.19; 'import': 0.22; 'aug': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'fairly': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'equivalent': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'on,': 0.29; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'implicit': 0.31; 'class': 0.32; 'stuff': 0.32; 'option': 0.32; 'quite': 0.32; 'subject:the': 0.34; 'received:74.125.82': 0.34; 'problem': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; '(we': 0.36; 'adjust': 0.36; 'done': 0.36; 'doing': 0.36; "didn't": 0.36; 'performance': 0.37; 'implement': 0.38; 'skip:& 10': 0.38; 'issue': 0.38; 'received:74.125': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'easy': 0.60; 'up,': 0.60; 'break': 0.61; 'kind': 0.63; 'to:addr:gmail.com': 0.65; 'within': 0.65; 'here': 0.66; 'functions)': 0.84; 'slowed': 0.84; 'bless': 0.91 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:from:date :message-id:subject:to:cc:content-type; bh=yjBlzz82DklNDL5UGcMmZ+ZBU1gHk/8pc6uEsIpSp/A=; b=evkZB/qFSRwiQi7CbkQd5VsGq4PIHdogWbJrMJbX8ShBpV/b494CrjkBlOjTANi3wA Jf7BYOQ9QTvdxcG/vIUbLwtPx2g8Usajb4x09tqLEUj7i2Iy5O4IjSasXjG/D3D+2515 ZnbP7zd0XB3TWK88OHqdFgfhm6vDMzimQeQz5s+ftMi9uuEKSoxWkdDc9cXTcepbFBlb mYddrla79aWFTG3ULnFGMXbRY/Jxe9c76+uTfQlZUY/5qoQY5gviCMF7nfRmr4/SP33L lHJi8XZDXArSkEELcTQYGx/G6OAp9dZ8/wkyt5DbiSlVA92N6keYIzbNNzgYZOmlFy10 kxaQ== X-Gm-Message-State: ALoCoQmLH3oqCsb4KYGf623nZUQx4EZrwcbNL8a74cuKZY+d7VOLstiNHGlmNgYdTZ10w4LvKzFm X-Received: by 10.180.38.39 with SMTP id d7mr298120wik.24.1408380823315; Mon, 18 Aug 2014 09:53:43 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Chris Kaynor Date: Mon, 18 Aug 2014 09:53:23 -0700 Subject: Re: Module-level functions and the module type To: Chris Angelico Content-Type: multipart/alternative; boundary=e89a8f646fcfaebf0b0500ea3686 Cc: "python-list@python.org" 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: 118 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1408380830 news.xs4all.nl 2967 [2001:888:2000:d::a6]:52215 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:76486 --e89a8f646fcfaebf0b0500ea3686 Content-Type: text/plain; charset=UTF-8 On Sun, Aug 17, 2014 at 8:15 AM, Chris Angelico wrote: > In a class definition, you have explicit state parameters on your > functions - 'self': > > class C: > def foo(self, arg): > # blah blah > > At module level, there's equivalent state - the function "knows" what > module it came from - but it's implicit: > > def foo(arg): > # blah blah > > print(foo.__globals__) > > How hard would it be to unify these, and make modules into classes? > This would then allow stuff like properties, metaclasses, and so on, > all with exactly the same semantics as they have in classes. > > Obviously this would be a huge backward-compatibility break if it > happened everywhere, but what I'm looking at here is a way to > basically bless this kind of concept: > > # spam.py > class RealSpam: > # module contents here > > import sys > sys.modules[__name__] = RealSpam() > So the question is: Why is state implicit in one and explicit in the > other? Which option is really the better way to do things? > As a heads up, we did this at work (mostly, just to support module-level properties) by using an import hook to adjust the type of object loaded from .py files. At the root, it was fairly easy to implement (though quite a bit of boiler code), however the main problem we ran into was performance - doing so slowed down all module accesses. The other issue with the way we did it (we didn't pass in the module to all functions) was that global accesses would not access the properties, so work-arounds had to be done to access them within the module itself. Chris --e89a8f646fcfaebf0b0500ea3686 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On S= un, Aug 17, 2014 at 8:15 AM, Chris Angelico <rosuav@gmail.com> wrote:
In a class definition, you have explicit state parameters = on your
functions - 'self':

class C:
=C2=A0 =C2=A0 def foo(self, arg):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 # blah blah

At module level, there's equivalent state - the function "knows&qu= ot; what
module it came from - but it's implicit:

def foo(arg):
=C2=A0 =C2=A0 # blah blah

print(foo.__globals__)

How hard would it be to unify these, and make modules into classes?
This would then allow stuff like properties, metaclasses, and so on,
all with exactly the same semantics as they have in classes.

Obviously this would be a huge backward-compatibility break if it
happened everywhere, but what I'm looking at here is a way to
basically bless this kind of concept:

# spam.py
class RealSpam:
=C2=A0 =C2=A0 # module contents here

import sys
sys.modules[__name__] =3D RealSpam()

So the question is: Why is state implicit in one and explicit in the
other? Which option is really the better way to do things?
=
=C2=A0
As a heads up, we did this at work (mostly, just to s= upport module-level properties) by using an import hook to adjust the type = of object loaded from .py files. At the root, it was fairly easy to impleme= nt (though quite a bit of boiler code), however the main problem we ran int= o was performance - doing so slowed down all module accesses.

The other issue with the way we did it (we didn't p= ass in the module to all functions) was that global accesses would not acce= ss the properties, so work-arounds had to be done to access them within the= module itself.

Chris
--e89a8f646fcfaebf0b0500ea3686--