Inheritance

54 views
Skip to first unread message

Nancy

unread,
Jul 10, 2012, 1:15:18 PM7/10/12
to mich...@googlegroups.com

I'm experimenting with inheritance and could use some guidance.

Here's what I have, after fighting off error messages on previous attempts. :-)

import ftplib
class myftp(ftplib.FTP):
    def __init__(self):
        self.FTP = ftplib.FTP()   


This appears to do what I want it to do* (see below).
But should I be setting it up differently than shown above?

*As an example, I can do the following and it works.

ftp = myftp()
ftp.connect(host)
ftp.login(user, passwd)
# etc
ftp.close()

Thanks for any input, thanks!
-Nancy

Jay Wren

unread,
Jul 10, 2012, 1:30:03 PM7/10/12
to mich...@googlegroups.com
That works, but you are allocating and initializing the FTP member and never using it.

import ftplib
class myftplib(ftplib.FTP):
    def __init__(self):
        pass

would work just the same, and for that matter, so would:

class myftplib(ftplib.FTP):
  pass

--


--
You received this message because you are subscribed to the Google Groups "Michigan Python Users Group" group.
To view this discussion on the web visit https://groups.google.com/d/msg/michipug/-/co5-M_x2mD4J.
To post to this group, send email to mich...@googlegroups.com.
To unsubscribe from this group, send email to michipug+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/michipug?hl=en.

Ryan Burns

unread,
Jul 10, 2012, 2:14:29 PM7/10/12
to mich...@googlegroups.com
If you wanted to pass some of your own arguments to your own init and also use the original init you can also do that:

import ftplib
class MyFTPLib(ftplib.FTP):
   def __init__(self,custom,host):
      ftplib.FTP.__init__(self,host)
      self.custom = custom


This stackoverflow thread has some more discussion about this kind of thing:


I'm sure you can also take a variable number of arguments to match the FTP init precisely, I think you would say something like *args and then pass that to the FTP init?


- As an aside, the Python style guide recommends using CapWords for class names.





Paul Woolcock

unread,
Jul 10, 2012, 1:26:36 PM7/10/12
to mich...@googlegroups.com
It would be better to use `super` instead:

    import ftplib
    class MyFTP(ftplib.FTP):
        def __init__(self):
            super(MyFTP, self).__init__()

Here is the python documentation on `super`: http://docs.python.org/library/functions.html?highlight=super#super



--
You received this message because you are subscribed to the Google Groups "Michigan Python Users Group" group.
To view this discussion on the web visit https://groups.google.com/d/msg/michipug/-/co5-M_x2mD4J.
To post to this group, send email to mich...@googlegroups.com.
To unsubscribe from this group, send email to michipug+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/michipug?hl=en.



--
Paul Woolcock
Twitter @pwoolcoc
Github @pwoolcoc


Nancy

unread,
Jul 10, 2012, 2:57:03 PM7/10/12
to mich...@googlegroups.com
 
I appreciate the feedback! So far, now, I have:
 
import ftplib
class MyFTP(ftplib.FTP):
    def __init__(self):
        pass
 
I do plan to do some "custom" but am not there yet :)
 
I did try this (in place of "pass"):  super(MyFTP, self).__init__()
TypeError: super() argument 1 must be type, not classobj
 
Thanks again,
-Nancy

 

Nancy

unread,
Jul 10, 2012, 3:02:16 PM7/10/12
to mich...@googlegroups.com

Also I should have mentioned (though don't think it's been a factor yet):

Py 2.6 on Win

-Nancy

--
You received this message because you are subscribed to the Google Groups "Michigan Python Users Group" group.
To view this discussion on the web visit https://groups.google.com/d/msg/michipug/-/5iTvwqpn_mIJ.

Ryan Burns

unread,
Jul 10, 2012, 3:11:24 PM7/10/12
to mich...@googlegroups.com
I tried this exact code;

import ftplib
class MyFTP(ftplib.FTP):
    def __init__(self):
        super(MyFTP,self).__init__()
    

And it didn't throw any errors.  A quick google indicated this error can occur if the parent class you are inheriting from is not a new-style class.  I'm not sure why that would be the case in your case, although I am on 2.7.  

Ryan Burns

unread,
Jul 10, 2012, 3:18:32 PM7/10/12
to mich...@googlegroups.com
Ahaha, the ftplib.FTP class is an old style class in the 2.6 release.

Huh, I checked both the 2.6.8 and 2.7.3 ftplib source and it's an old style class in both.  I'm not sure why it worked for me.


You can still use ftplib.FTP.__init__(self)      - super() only really matters if you have multiple inheritance (feel free to disabuse me of that notion if it's not the case).


-Ryan

Ryan Burns

unread,
Jul 10, 2012, 3:23:01 PM7/10/12
to mich...@googlegroups.com
Oh, it worked for me because I didn't create an instance of MyFTP when I tested it, and that is of course when the error occurs.


Long story short: you can't use super() if you're inheriting from ftplib.FTP in that version of Python, but in general it's the way to go.

Nancy

unread,
Jul 10, 2012, 4:11:27 PM7/10/12
to mich...@googlegroups.com

I appreciate the clarification on super() - Thanks!

Reply all
Reply to author
Forward
0 new messages