New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
file.tell() returns Long usually, Int if subclassed #46864
Comments
Compare: >>> class y(file):
... def __init__(self, name):
... file.__init__(self, name)
... def __len__(self):
... self.seek(0, 2)
... return self.tell()
...
>>> n = y('/tmp/longfile')
>>> len(n)
364773376 Versus: >>> class z:
... def __init__(self, name):
... self.f = file(name, 'r')
... def __len__(self):
... self.f.seek(0, 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 Because: >>> x = file('/tmp/longfile')
>>> type(x.tell())
<type 'long'> |
This is because y is a new-style class (because file is) and z is not. >>> class z(object):
... def __init__(self, name):
... self.f = file(name, 'r')
... def __len__(self):
... self.f.seek(0, 2)
... return self.f.tell()
>>> x = z('/tmp/longfile')
>>> len(x)
[Whatever it is] |
Thanks Benjamin, that is a very interesting feature that I can't find thanks, |
The documentation is still not very good about documenting the |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: