static python classes ?

Neil Cerutti horpner at yahoo.com
Wed Jun 20 11:30:21 EDT 2007


On 2007-06-20, Alex Martelli <aleax at mac.com> wrote:
> Neil Cerutti <horpner at yahoo.com> wrote:
>
>> In C++ they are used most often for factory functions, since they
>> conveniently have access to the class's private members, and
>> don't want or need an existing instance. Python seems to have
>> adopted this use-case (ConfigParser, for example), but without a
>> need for it (code organization?).
>
> What staticmethod does ConfigParser have?  Can't recall one
> offhand.

I misremembered which module I had recently seen it in: it was
datetime, not ConfigParser. And ('datetime.today') is, as you
point out, a class method rather than a static method.

Thanks for the correction.

> I think Python more commonly uses classmethod, rather than
> staticmethod, for factories (i.e. a la Smalltalk, not a la
> C++).  In that case the advantage wrt a function _is_ there:
> when you subclass, you can get instances of the new class,
> rather than the base class, from the factory:
>
>>>> class zap(dict): pass
> ... 
>>>> z = zap.fromkeys(range(4))
>>>> z
> {0: None, 1: None, 2: None, 3: None}
>>>> type(z)
><class '__main__.zap'>
>>>>
>
> If dict_fromkeys was a plain function (or if fromkeys was a
> staticmethod rather than a classmethod of dict) then z would be
> an instance of dict, rather than one of zap (unless you also
> specifically passed the desired class, kind of cumbersome).

Thanks. That makes much more sense than my misunderstanding did. ;)

-- 
Neil Cerutti
Ask about our plans for owning your home --sign at mortgage company



More information about the Python-list mailing list