Subclassing zipfile (new style class)

L

Larry Bates

I'm trying to learn about subclassing new style classes and the first project I
went to do needs to subclass zipfile to add some methods.


Why does this:

import zipfile
class walkZip(zipfile):
pass


if __name__ == "__main__":
print "Hello World"

Traceback (most recent call last):
File "<string>", line 192, in run_nodebug
File "<module1>", line 2, in <module>
TypeError: Error when calling the metaclass bases
module.__init__() takes at most 2 arguments (3 given)
Thanks in advance.

-Larry
 
F

Farshid Lashkari

Larry said:
import zipfile
class walkZip(zipfile):
pass


if __name__ == "__main__":
print "Hello World"

Traceback (most recent call last):
File "<string>", line 192, in run_nodebug
File "<module1>", line 2, in <module>
TypeError: Error when calling the metaclass bases
module.__init__() takes at most 2 arguments (3 given)

In your code 'zipfile' is a module, not a class, so you cannot inherit
from it. I believe this is what you are trying to accomplish:

class walkZip(zipfile.ZipFile):
pass

-Farshid
 
T

Tom Brown

I'm trying to learn about subclassing new style classes and the first
project I went to do needs to subclass zipfile to add some methods.


Why does this:

import zipfile
class walkZip(zipfile):
pass


if __name__ == "__main__":
print "Hello World"

Traceback (most recent call last):
File "<string>", line 192, in run_nodebug
File "<module1>", line 2, in <module>
TypeError: Error when calling the metaclass bases
module.__init__() takes at most 2 arguments (3 given)

I think because you need to do this:

from zipfile import ZipFile
class walkZip(ZipFile):
pass


-Tom
 
M

Matimus

import zipfile
class walkZip(zipfile):
pass

if __name__ == "__main__":
print "Hello World"


`zipfile' is the name of the module, not a class. What you probably
want is this:

Code:
import zipfile
class WalkZip(zipfile.ZipFile):
    pass

if __name__ == "__main__":
    print "hello world"

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top