[Python-Dev] Re: Guido's Magic Code was: inline sort option
Christian Tanzer
tanzer at swing.co.at
Thu Oct 30 12:39:48 EST 2003
> Darn -- it WOULD be better in some cases if one could ONLY call
> a method on the class, NOT on an instance when the call would in
> any case ignore the instance. Calling dict.fromkeys(L3) is wonderful,
> the problem is that you can also call it on a dict instance, and THAT
> gets confusing. Similarly, calling list.sorted(iterable) is wonderful,
> but calling it on a list instance that gets ignored, L1.sorted(iterable),
> could perhaps be confusing.
Then why don't you use a custom descriptor which raises an exception
when an instance is passed in? Like:
def __get__(self, obj, cls):
if obj is None:
return new.instancemethod(self.classmeth, cls)
else:
raise TypeError, \
"Calling %s on instance %s ignores instance" % \
(self.classmeth, obj)
--
Christian Tanzer http://www.c-tanzer.at/
More information about the Python-Dev
mailing list