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

Marko Rauhamaa marko at pacujo.net
Sun Aug 3 11:52:36 EDT 2014


Roy Smith <roy at panix.com>:

>  Marko Rauhamaa <marko at pacujo.net> wrote:
>
>> I've reached a point where I think classes are a superfluous OO
>> concept. You only need objects.
>
> comp.lang.javascript is over that way -->

Thanks for the insight. I'm currently more absorbed by comp.lang.scheme,
though.

Now, Python is ducktyped. It is (rightly) considered bad taste to
consult the datatype (class) of an object. Compare these two
definitions:

   class Point:
       def __init__(self, x, y):
           self.x = x
           self.y = y

       def x(self):
           return self.x

       def y(self):
           return self.y

and:

   class Object: pass

   def Point(x, y):
       self = Object()
       self.__dict__ = dict(x=lambda: x, y=lambda: y)
       return self

For all practical purposes, the two definitions are identical even
though the latter doesn't specify any class. Inheritance needs to be
addressed, but that can be taken care of without classes as well.

Obviously, Python's syntax makes it convenient to deal with classes, and
there's no practical downside to it. However, conceptually, classes are
unnecessary baggage and at odds with ducktyping.


Marko



More information about the Python-list mailing list