Close. You want an instance of the specific type of Coconut subclass. Instead, 
what you are doing is using your base class.

# create an instance of our SouthAsian Coconut subclass
anAsianCoconut = SouthAsian()

# append the instance
stack.add_coconut(anAsianCoconut)

What you are appending to your list are actual instances of a class, as opposed 
to just a name.

__repr__() works by returning any kind of string to be the representation:


class SouthAsian(Coconut):

    weight = 3.0

    def __repr__(self):
        return "<SouthAsian (Coconut) - weight: %0.1f >" % self.weight



On Sep 22, 2013, at 4:10 PM, matthew park wrote:

> Hi Justin, 
> Thank you so much for your kind explaining. it's awesome.
> 
> Since I change the code to 
>     def add_coconut(self, name):
>         if not isinstance(name, Coconut):
>             raise AttributeError("%s isn't type of coconut can be 
> stored."%(name))
>         self.coconuts.append(name)
> 
> 
> So the way I implement my code is call
> SouthAsian = Coconut()
> stack = Inventory()
> stack.add_coconut(SouthAsian)
> 
> Is this correct? 
> 
> Also if I want to print out the list type object of the stack, how to use 
> __repr__ for 
> this case, I know the way for the string type but not for the list type.
> 
> Thanks again!!
> 
> Matt
> 
> 
> On Sat, Sep 21, 2013 at 7:35 PM, Justin Israel <[email protected]> wrote:
> Seems like that approach will work just fine, aside from a couple small 
> adjustments:
> 
> In your add_coconut method, you will want to be checking for an instance of 
> Coconut, instead of Inventory.
> 
> You could also increase the efficiency of your total_weight() method by 
> tracking the weight from inside of the add_coconut() call. 
> This way, all you have to do is return a value instead of looping the 
> inventory every time.
> 
> With the previous suggestion, it would then be best to make the coconuts list 
> a private attribute so that people wouldn't be modifying the
> list directly and messing with your optimized tracking.
>     self.__coconuts = []
> 
> If they ask you later to track the coconuts by their type, you may find 
> yourself wanting to switch to a dictionary, where the key would be the 
> product type (Coconut)
> and the value could be a list of those coconuts. 
> 
> 
> 
> On Sep 22, 2013, at 1:41 PM, matthew park wrote:
> 
>> Hi there I am doing a assignment and the code should be simple as soon as I 
>> get 
>> what the question is meaning.
>> 
>> The assignment is
>> Create Inventory class that tracks different types of coconuts from around 
>> the world. The different types of coconuts must have these weight attribute 
>> values:
>> 
>> Type
>> 
>> Weigt
>> 
>> South Asian
>> 
>> 3
>> 
>> Middle Eastern
>> 
>> 2.5
>> 
>> American
>> 
>> 3.5
>> 
>> The inventory class must have the following methods:
>> 
>> add_coconut() accepts a coconut as an argument. Other types throw an 
>> AttributeError.
>> 
>> total_weight() provides the total weight of coconuts.
>> 
>> 
>> 
>> 
>> 
>> 
>> The code that I am thinking of now is below
>> 
>> 
>> 
>> 
>> 
>> class Inventory(object):
>>     def __init__(self):
>>         self.coconuts = []
>> 
>>     def add_coconut(self, cocoType):
>>         if not isinstance(cocoType, Inventory):
>> 
>> 
>> 
>> 
>>             raise AttributeError("%s isn't type of Coconut can be 
>> added"(cocoType))
>>         self.coconuts.append(cocoType)
>> 
>>     def total_weights(self):
>>         totalWeight = 0.0
>> 
>> 
>> 
>> 
>>         for each in coconuts:
>>             totalWeight += each.weight
>> 
>> 
>> class Coconut(object):
>>     weight = 0.0
>> 
>> class SouthAsian(Coconut):
>> 
>> 
>> 
>> 
>>     weight = 3.0
>> 
>> class MiddleEastern(Coconut):
>>     weight = 2.5
>> 
>> class American(Coconut):
>>     weight = 3.5
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> Thank you so much for your help!
>> 
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Python Programming for Autodesk Maya" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> For more options, visit https://groups.google.com/groups/opt_out.
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to