Newbie: Functions and class

Alex Martelli aleax at aleax.it
Fri Oct 31 04:22:21 EST 2003


Shu-Hsien Sheu wrote:

> Hi Alex,
> 
> Great thanks! What u've suggested was exactly what I was looking for.
> Too bad it's such a trivial question:((

"The only bad question is the one not asked":-).

> 
> I have another trivial question though. Why won't the following work?

Works fine, actually, just probably doesn't do what you THINK it does:-).

> class hittable(object):
>     def __init__(self):
>        self = [[], [], [], []]

this rebinds local variable self in the __init__ metod (to a list of
four lists), and that's all.  Doesn't affect any INSTANCE of class
hittable, for example.  What are you trying to do?!  If you want
"hittable" to return a list of lists then it must be a function,
not a class, obviously:

def hittable(): return [[], [], [], []]

if you want each instance of class hittable to start out with
an ATTRIBUTE holding a list of four empty lists then SAY so:

class hittable(object):
    def __init__(self):
       self.putsomenamehere = [[], [], [], []]


Alex





More information about the Python-list mailing list