the first arg to super() must be a type, not a class obj?

M

metaperl

On p.282 of "Python Cookbook" and in the Python docs on calling super:
http://www.python.org/download/releases/2.2.3/descrintro/#cooperation

it is clear that the first argument to super is a class and not a type.
However, for the code below, I am getting an error when attempting to
provide a class as my first argument and don't understand why. Also,
since this is my first attempt at writing anything of any seriousness
in Python, any other feedback is welcome.

from ftputil import *
import re

from urlparse import urlparse

class ftputilx(FTPHost):
parms = {
'user': '',
'pass': '',
'site': '',
'path': '',
'file': '',
'action': '',
'lcd': '',
'autogo': True,
'debuglevel': 0,
'type': 'b'
}

def bindparms(self, **kwargs):
for k, v in kwargs.items():
self.parms[k] = v

def __init__(self, url, **kwargs):
_s, site, self.parms['path'], _p, _q, _f = urlparse(url)
if '@' in site:
rxs = r'(.+):(.+)@(.+)'
rxc = re.compile(rxs)
rxm = rxc.match(site)
(kwargs['user'],kwargs['pass'],kwargs['site']) =
rxm.group(1), rxm.group(2), rxm.group(3)
self.bindparms(**kwargs)
super(ftputilx, self).__init__()


if __name__ == '__main__':
ftp =
ftputilx("ftp://anonymous:[email protected]/pub/linux/")
print ftp.listdir(ftp.curdir)
 
D

Dennis Lee Bieber

However, for the code below, I am getting an error when attempting to

Ah, an error? Pray tell, what is the error you are receiving?
super(ftputilx, self).__init__()

Could it be, perchance, that you forgot to pass all the arguments to
the __init__() call?

super(ftputilx, self).__init__() should translate to
FTPHost.__init__(self)... Where are host, user, password, account?

host = ftputil.FTPHost(host, user, password, account,
session_factory=ftplib.FTP)

I propose that you need

super(ftputilx, self).__init__(host, user, password)

at a minimum.

Oh, BTW -- it looks like you are sharing the parms dictionary among
all instances of the class. And if it is being filled properly, you need

super(ftputilx, self).__init__(parms["host"], parms["user"],
parms["password"])

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
G

Georg Brandl

metaperl said:
On p.282 of "Python Cookbook" and in the Python docs on calling super:
http://www.python.org/download/releases/2.2.3/descrintro/#cooperation

it is clear that the first argument to super is a class and not a type.
However, for the code below, I am getting an error when attempting to
provide a class as my first argument and don't understand why. Also,
since this is my first attempt at writing anything of any seriousness
in Python, any other feedback is welcome.

"super" only works for new-style classes.

You could make "ftputilx" new-style by inheriting from object and FTPHost,
but FTPHost is still old-style, and I don't know if that's okay with super.

So in your case it would be advisable to use

FTPHost.__init__(self)

instead of

super(ftputilx, self).__init__()

Georg
 
S

Steve Holden

metaperl said:
On p.282 of "Python Cookbook" and in the Python docs on calling super:
http://www.python.org/download/releases/2.2.3/descrintro/#cooperation

it is clear that the first argument to super is a class and not a type.
However, for the code below, I am getting an error when attempting to
provide a class as my first argument and don't understand why. Also,
since this is my first attempt at writing anything of any seriousness
in Python, any other feedback is welcome.
I believe it needs to be a class whose ultimate ancestor is "object",
and not an "old-style" class. This is probably why the docs suggest a
type is required.

regards
Steve
 
S

Steve Holden

Andre said:
Another thing: how does super() work wrt. multiple inheritance? It seems
like it returns only the first superclass.
The whole point of super is that it returns the first superclass in the
MRO that *follows* the class of the (first) argument. It's this
behaviour that makes super() useful in multiple inheritance situations
(if you believe that super() is in fact useful - there are those who
have their doubts).

regards
Steve
 

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,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top