[Tutor] Py 2.4.4: Inheriting from ftplib.FTP()
Alan Gauld
alan.gauld at yahoo.co.uk
Thu Jun 16 12:40:39 EDT 2016
On 16/06/16 16:38, boB Stepp wrote:
> class FTPFiles(FTP, object):
> """FTP files to Windows server location(s)."""
OK, First comment. You describe this as an action(verb)
rather than a noun. Classes should represent nouns
(objects) not actions. FTP represents a protocol
connection with which you can do things (one of
which is put/get files) you class should be the same.
(Remember inheritance m,eans you are imp0lementing
an "is a" relationship. So FTPFiles is an FTP...or
should be.)
> def __init__(self, host=server_ip, user=user, passwd=passwd):
> """Initialize FTPFiles object. Normally the defaults will be used."""
>
> super(FTPFiles, self).__init__(host, user, passwd)
> self.host = host
> self.user = user
> self.passwd = passwd
>
> def print_welcome_msg(self):
> """Print welcome message sent by FTP server."""
> print self.getwelcome()
> 1) FTP from ftplib appears to be an old-style class.
Using Python 2.4 that's not too surprising, FTP is an
old module.
> 4) I have to use "super(FTPFiles, self).__init__(host, user, passwd)"
> or I cannot successfully inherit from the FTP() class. Also, "self"
> apparently must come AFTER "FTPFiles" in the super expression.
That's the v2 super(). v3 super is far supeerior.
> 4) As this class stuff is mostly treading new ground for me, am I
> doing anything that I should not be doing or should do in a more
> preferred way? Keep in mind that I am just starting on this code
> today.
I'll leave someone with more ftplib experience to answer the other
points but the question I'd ask is what are you planning to add to FTP?
Creating a new class by inheritance implies that you are going to be
extending (or specializing) its capabilities in some way. What are you
planning to extend/specialize? For example are you going to limit it to
manipulating files only? ie prevent listing directories say?
You need to know what you are changing to warrant using inheritance.
Inheritance is a powerful tool but it carries lots of potential for
problems too, especially if you plan on mixing your new class in with
the old one (or sharing it with others who will) - you need to be
careful to abide by the Liskov substitution principle (LSP).
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list