Correct type for a simple "bag of attributes" namespace object

Marko Rauhamaa marko at pacujo.net
Mon Aug 4 01:41:07 EDT 2014


Steven D'Aprano <steve+comp.lang.python at pearwood.info>:

> Marko Rauhamaa wrote:
>
>> I've reached a point where I think classes are a superfluous OO concept.
>> You only need objects.
>
> I don't know whether "superfluous" is correct, but they certainly are
> *optional*. There are at least two types of object oriented programming:
> class-bases, and prototype-based.

And I'm talking about a third kind: object-based. It is in active
(albeit limited) use in scheme: <URL: http://irreal.org/blog/?p=40>. I'm
currently using the principle in a project of mine.

In Java, you use anonymous classes for the same thing. In Python, you
can think of the principle as one-time classes. So instead of writing:

    class A:
        def __init__(self, x, y, z):
            self.x = x
            self.d = y * y + z * z

        def f(self):
            return self.x - self.d

you write:

    def A(x, y, z):
        d = y * y + z * z

        class Anonymous:
            def f(self):
                return x - d

        return Anonymous()

Now, if you always did this, you would notice that classes are
unnecessary clutter and would call for syntax like this:

    def A(x, y, z):
        d = y * y + z * z
        return object:
            def f():
                return x - d


Marko



More information about the Python-list mailing list