[Tutor] Py 2.4.4: Inheriting from ftplib.FTP()
boB Stepp
robertvstepp at gmail.com
Fri Jun 17 12:07:12 EDT 2016
I guess I did not emphasize enough that I was just starting to think
through this. The code I posted yesterday was more in the way of
exploratory code, trying to understand how to implement inheritance,
using new-style classes (I am trying to integrate the work with Python
3 at home as much as possible with what I am forced to in Python 2.4
at work where I have no choices as to Python version.).
On Fri, Jun 17, 2016 at 10:31 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> Some futher thoughts:
>
> On Thu, Jun 16, 2016 at 10:38:13AM -0500, boB Stepp wrote:
>> class FTPFiles(FTP, object):
>> """FTP files to Windows server location(s)."""
>>
>> 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
>
> Do you actually need to record these? Once they're used to establish a
> connection and login, what are they for?
No, I was just spelling everything out for myself while I was trying
to figure out why my original code was not working. All the things I
said I "learned" were not in my earlier versions. These lines have
already been excised as I am getting closer to something I might
actually want to implement. Also, now that I have had the opportunity
to play around with inheritance, I don't see any need to specialize
the FTP class as I mention in my response to Alan I just sent out.
>
>> def print_welcome_msg(self):
>> """Print welcome message sent by FTP server."""
>> print self.getwelcome()
>
> The getwelcome method already prints the message, which is a bad design.
> But only if debugging is true. So I would approach this in one of two
> ways:
The only point of this method was to ensure that I was actually
connected to my server. During earlier versions, things were acting
weird and I was not understanding what was going on. So I wanted to
be certain I was in fact connected; it turned out I was not! This
also has been excised today.
> (1) Delegate to the existing getwelcome method:
>
> def print_welcome_msg(self):
> save_flag = self.debugging
> self.debugging = True
> try:
> x = self.getwelcome() # prints the msg
> finally:
> self.debugging = save_flag
>
>
> (2) Re-implement the print functionality.
>
> def print_welcome_msg(self):
> print "*welcome*", self.welcome
>
>
> (3) If you really want to be paranoid, use:
>
> print "*welcome*", self.sanitize(self.welcome)
>
>
> although I don't think it's likely to make any real difference in
> practice.
>
> (The sanitize method makes a feeble attempt to hide the password.)
Some interesting ideas to aid debugging. Thanks!
>> What I learned today:
>>
>> 1) FTP from ftplib appears to be an old-style class.
>>
>> 2) I can't just use "class FTPFiles(FTP)" or I will be using old-style classes.
>
> Is this a problem? Old-style classes are good enough for many purposes.
Not really. I am just trying to consolidate the studies I am doing at
home with Python 3 as much as I can when I am working with Python 2.
>> 3) I need to use "class FTPFiles(FTP, object)" to use new-style
>> classes and "object" must come AFTER "FTP".
>>
>> 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.
>
> For old-style classes, don't use super because it doesn't work. Instead
> just write:
>
> FTP.__init__(self, host, user, passwd)
Yeah, if I just stuck to old-style, things would have been easier.
> That means that you cannot engage in multiple inheritence with new-style
> classes, but MI is a serious pain to get right, and 99% of the time it
> is overkill, so you're not missing much.
Right now just the simpler OOP stuff is a "serious pain" while I am
still in the relatively early stages of learning. ~(:>))
boB
More information about the Tutor
mailing list