Reputation: 3
I am trying to write a class that inherits from mmap as follows:
from mmap import mmap
class SBFMap(mmap):
def __init__(self, filename):
f=open(filename, 'rb')
fn = f.fileno()
super().__init__(fn, 0, access = mmap.ACCESS_READ)
As you can guess, part of my class's functionality is to hide the file opening/closing inside my __init__. i then try to get an SBFMap object like so:
from SBFMap import SBFMap
filename = "name\of\file"
mymap = SBFMap(filename)
I get this error:
File "SBFReader.py", line 22 in <module> main()
File "SBFReader.py", line 7, in main mymap=SBFMap(filename)
TypeError: an integer is required (got type str)
Press any key to continue . . .
It appears like instead of python calling SBFMap's __init__ and letting me call mmap's __init__ when I'm ready to, python is trying to call mmap's __init__. How do I fix this?
Upvotes: 0
Views: 130
Reputation: 149075
Because there are two different special methods that are involved in object creation: __new__
that creates and object and __init__
that configures it.
Generally Python mutable classes use a default __new__
that ends calling object.__new__
to create a new python object and accept any arguments.
But some classes (notably non mutable ones) do override __new__
. In that case, you must implement one in your class and call the one from the parent class with the expected signature.
Once the object will be constructed, Python will eventually call the __init__
method for the actual object.
Here you should do:
class SBFMap(mmap):
def __new__(cls, filename):
f=open(filename, 'rb')
fn = f.fileno()
return super().__new__(cls, fn, 0, access = mmap.ACCESS_READ)
But beware, if the parent class expected its __init__
method to be called, you would still have to implement one in your class that calls the parent __init__
with the expected signature.
Upvotes: 1