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

Marko Rauhamaa marko at pacujo.net
Mon Aug 4 02:57:46 EDT 2014


Ian Kelly <ian.g.kelly at gmail.com>:

> In my experience, 99% of the time when anonymous classes are used,
> they only contain one method. [...]
>
>>     def A(x, y, z):
>>         d = y * y + z * z
>>
>>         class Anonymous:
>>             def f(self):
>>                 return x - d
>>
>>         return Anonymous()
>
> And it's the same thing here.  This isn't an interface.  It's a
> function, so just return a function and be done with it.

My one-time classes usually have more than one method:

    def query(self, domain_name, record_type, listener, xid = None):
        ...
        client = self
        class Operation:
            def cancel(self):
                if key in client.opmap and client.opmap[key] is self:
                    del client.opmap[key]
                def callback():
                    client.log('CANCELED')
                    listener(client.CANCELED, None)
                client.mux.schedule(callback)
            def notify(self, verdict, records):
                client.log('verdict = {}'.format(verdict))
                listener(verdict, records)
        operation = Operation()
        self.opmap[key] = operation
        ...


Marko



More information about the Python-list mailing list