This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: file.tell() returns Long usually, Int if subclassed
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.4
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, esrever_otua, georg.brandl
Priority: normal Keywords:

Created on 2008-04-11 02:45 by esrever_otua, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg65331 - (view) Author: Darryl Dixon (esrever_otua) Date: 2008-04-11 02:45

>>> class y(file):
...     def __init__(self, name):
...         file.__init__(self, name)
...     def __len__(self):
..., 2)
...         return self.tell()
>>> n = y('/tmp/longfile')
>>> len(n)


>>> class z:
...     def __init__(self, name):
...         self.f = file(name, 'r')
...     def __len__(self):
..., 2)
...         return self.f.tell()
>>> x = z('/tmp/longfile')
>>> len(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: __len__() should return an int


>>> x = file('/tmp/longfile')
>>> type(x.tell())
<type 'long'>
msg65333 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-04-11 02:58
This is because y is a new-style class (because file is) and z is not.
If the return type of new-style classe's len is not an int, Python tries
to convert it, which is why that works for the first example. However,
that conversion doesn't happen in old-style classes. Try:

>>> class z(object):
...     def __init__(self, name):
...         self.f = file(name, 'r')
...     def __len__(self):
..., 2)
...         return self.f.tell()
>>> x = z('/tmp/longfile')
>>> len(x)
[Whatever it is]
msg65334 - (view) Author: Darryl Dixon (esrever_otua) Date: 2008-04-11 03:47
Thanks Benjamin, that is a very interesting feature that I can't find
documented anywhere. Is there perhaps a documentation bug that can arise
from this? There are various places where the differences between old
and new-style classes are documented to a greater or lesser degree, eg:
Is this documented somewhere currently that I missed, or could it be
incorporated somewhere?

msg65349 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-04-11 11:57
The documentation is still not very good about documenting the
differences between new and old style classes. Perhaps this is something
which could go under the __len__ entry in "Special Method Names."
Date User Action Args
2022-04-11 14:56:33adminsetgithub: 46864
2008-04-11 11:57:17benjamin.petersonsetnosy: + georg.brandl
messages: + msg65349
2008-04-11 03:47:33esrever_otuasetmessages: + msg65334
2008-04-11 02:58:39benjamin.petersonsetstatus: open -> closed
resolution: not a bug
messages: + msg65333
nosy: + benjamin.peterson
2008-04-11 02:45:14esrever_otuacreate