Module-level functions and the module type
Chris Angelico
rosuav at gmail.com
Sun Aug 17 11:15:57 EDT 2014
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
More information about the Python-list
mailing list