Help with classes

Kaden sfam at mailandnews.com
Sun Dec 2 02:27:34 EST 2001


On Sun, 02 Dec 2001 06:21:24 GMT, Hans Nowak <wurmy at earthlink.net> wrote:

> Kaden wrote:

>> class Basket:
>> 
>>     def __init__(self,contents=None):
>>         self.contents = contents or []
>> 
>>     def add(self,element):
>>         self.contents.append(element)
>>         print "You put the", element, "in your basket."
>> 
>>     def remove(self,element):
>>         self.contents.remove(element)
>>         print "You remove the", element, "from your basket."
>> 
>>     def print_me(self):
>>         result = ""
>>         for element in self.contents:
>>             result = result + " " + `element`
>>         print "You look in your basket and see:" + result
>> 
>> class NiceBasket(Basket):
>> 
>>     def open(self):
>>         print "You open your basket."
>>         state = "open"
>> 
>>     def close(self):
>>         print "You close your basket."
>>         state = "closed"
>> 
>> Now then, the above class works.  However, I want to be able to add some
>> checks, such as checking to see if NiceBasket is closed when you try to
>> add soemthing to it.  I can't figure out how to do this.  

> By overloading methods. For example, add a new 'add' method to
> NiceBasket, that checks, and if the check is successful, it calls
> its parents' add method:
> 
>     def add(self, element):
>         if self.state == "open":
>             Basket.add(self, element)
>         else:
>             print "The basket is not open."

I was thinking along those lines and I was _sort_of_ close.  This is what
I had:

def add(Basket,element):
    if state == "closed":
        print "You need to open it first!"

As you can see, I had no idea how to properly call the Basket class
(which was what I was trying to ask in the first place, I just couldn't
phrase it right), and I had no idea how to have the 'else' call 'Basket',
which is why it's conspicuously missing.  At this point my mind went
completely blank and I gave up and made the post.

> I see that in your open and close methods, you use 'state' as a local
> variable. This won't work; 

I knew this, I just didn't know how to make it accessible.  I tried
declaring it outside any methods, just after the 'def NiceBasket()', but
it didn't like that.

> Initializing this state variable is probably a good idea too. To 
> do so, override the __init__ constructor in NiceBasket:
> 
>     def __init__(self, contents=None):
>         Basket.__init__(self, contents)
>         self.state = "closed"  # or open, depending on what you want

More than a good idea.  Required.  At least with my version of Python
(2.1.1).  I tried it without and it got mad:

 >>> from Basket import NiceBasket
 >>> b = NiceBasket(['eggs'])
 >>> b.open()
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "Basket.py", line 28, in open
     if self.state == "open":
 AttributeError: NiceBasket instance has no attribute 'state'
 >>> 

I know these errors look pretty stupid to the experienced readers, but I
didn't think it was too bad considering that the only tutorials I could
find that mentioned classes only dealt with communicating with other
members of the same class.  None of them ever talked about how to move
from one class to the other.  Except, of course, for the matter of
inheritance.  Other than that they didn't mention anything.

Anyway,  thanks for the help, Hans.  The information was very helpful.



More information about the Python-list mailing list