defining classes

LeRoy Lee l3e3e7 at hotmail.com
Fri Sep 2 16:09:03 EDT 2005


I have been searching for the answer to this as it will determine how I use 
classes.  Here are two bits of code.

class foo1:
    def __init__(self, i):
        self.r = i
        self.j = 5

>>h = foo1(1)
>>h.r
1
>>h.j
5


Now take this example

class foo2:
    def __init__(self):
        self.j = 5

>>h = foo2()
>>h.j
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: foo2 instance has no attribute 'j'

I can't figure out why it is working this way.  I figure I must be thinking 
about this wrong.  I was thinking that I could bind attributes to the class 
from within methods using the self prefix.  According to this example I can 
only when passing other info into the init.  Is there a rule that I am just 
not aware off?  Am I totally off base (I am not real experienced)?  What is 
the self prefix for then if not to bind up the tree?

Thanks,
LeRoy

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/




More information about the Python-list mailing list