[Tutor] Stupid newbie question

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sat Sep 24 00:41:04 CEST 2005



On Fri, 23 Sep 2005, Valone, Toren W. wrote:

> I am trying to noodle thru classes with python and I built the following
> class.

Hi Toren,

Ah.  Check your indentation: it appears that the definition of getday() is
within the body of the class initializer __init__().

What ends up happening is that getday() is no longer a method definition,
but an inner function definition.  Python makes it very easy to define
functions within functions, and what you have in effect is a function
definition within the __init__() method definition, which is probably not
what you want.

Tip: if possible, always use four spaces for your indentation to make this
error easier to see.  Don't skimp on this.  *grin*


Also note that Python's class system does not make using 'self' optional,
so when you're initializing the attributes of an instance in __init__:

    def __init__(self):
        remailfile = open('U:\Bounce20.txt', 'r')
        resendfile = open('resend.txt', 'w')
        EmailReport = open('erprt.txt', 'w')
        ...

you need to tell Python not to treat these as local variables assignments.

    def __init__(self):
        self.remailfile = open('U:\Bounce20.txt', 'r')
        self.resendfile = open('resend.txt', 'w')
        self.EmailReport = open('erprt.txt', 'w')
        ...


As a side note: escape the backslashes in your literal strings: otherwise,
you'll run into issues.  I'm assuming you're coming from a Java or C++
background, but if you need this point elaborated, please ask, and we'll
go into more detail.



If you have more questions, please feel free to ask.  Good luck!



More information about the Tutor mailing list