Namespaces are one honking great idea

BartC bc at freeuk.com
Fri Jul 1 10:49:15 EDT 2016


On 01/07/2016 15:13, Steven D'Aprano wrote:

> Sometimes we have a group of related functions and variables that belong
> together, but are not sufficiently unrelated to the rest of the module that
> we want to split them out into another file.

> Here's a proof of concept. I use a class with a custom metaclass like this:
>
>
> # Python 3 version
> class ExampleNS(metaclass=Namespace):
>     x = 1
>     y = []
>
>     def spam(n):
>         return 'spam'*n

> py> Example.spam(5)
> 'spamspamspamspamspam'


Why not just extend the capabilities of a class? I actually thought this 
would work until I tried it and it didn't:

class C():
     def fn():
         print ("Hi!")

C.fn()

The error message suggests Python knows what's going on. So why not just 
make it work?

(The main issue would be, if an instance X of C /was/ created, then 
X.fn() wouldn't work as there is no 'self' parameter for fn.)

-- 
Bartc




More information about the Python-list mailing list