Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #76433

Module-level functions and the module type

Date 2014-08-18 01:15 +1000
Subject Module-level functions and the module type
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.13070.1408288560.18130.python-list@python.org> (permalink)

Show all headers | View raw


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?

ChrisA

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Module-level functions and the module type Chris Angelico <rosuav@gmail.com> - 2014-08-18 01:15 +1000
  Re: Module-level functions and the module type Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-18 01:59 +1000
    Re: Module-level functions and the module type Chris Angelico <rosuav@gmail.com> - 2014-08-18 02:20 +1000

csiph-web