Memory error due to big input file
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Mon Jul 13 23:49:30 EDT 2009
On Mon, 13 Jul 2009 14:20:13 -0700, Aaron Scott wrote:
>> BTW, you should derive all your classes from something. If nothing
>> else, use object.
>> class textfile(object):
>
> Just out of curiousity... why is that? I've been coding in Python for a
> long time, and I never derive my base classes. What's the advantage to
> deriving them?
"Old style" classes (those whose base classes aren't derived from
anything) have a few disadvantages:
(1) Properties don't work correctly:
>>> class Parrot: # old-style class
... def __init__(self):
... self._x = 3
... def _setter(self, value):
... self._x = value
... def _getter(self):
... print "Processing ..."
... return self._x + 1
... x = property(_getter, _setter)
...
>>> p = Parrot()
>>> p.x
Processing ...
4
>>> p.x = 2
>>> p.x
2
In general, anything that uses the descriptor protocol, not just
property, will fail to work correctly with old-style classes.
(2) Classes using multiple inheritance with diamond-shaped inheritance
will be broken.
(3) __slots__ is just an attribute.
(4) super() doesn't work.
And, depending on whether you consider this a disadvantage or an
advantage:
(5) Special methods like __len__ can be over-ridden on the instance, not
just the class:
>>> class K:
... def __len__(self):
... return 0
...
>>> k = K()
>>> len(k)
0
>>> k.__len__ = lambda : 42
>>> len(k)
42
In their favour:
(1) Less typing.
(2) If you're not using descriptors, including property(), or multiple
inheritance with diamond diagrams, they work fine.
(3) They're (apparently) a tiny bit faster.
--
Steven
More information about the Python-list
mailing list