#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Trouble Returning a List of Custom Classes


    I would like to preface this with, I tried to search a couple different ways and quite find anything to help, as well as I am new to Python (which will become apparent).
    My goal is to have an Item class that is, for right now, a two part list with a name, and a size, and then an Inventory class that is a list of items. I have everything working as intended for the moment besides for one method in Inventory, who's purpose is to return the value of every entry in the Inventory. My code so far is as follows.

    Code:
    class Item(list):
    
        def __init__(self, name = "Default", size = 1):
            super(Item, self).__init__()
            x = [name, size]
            self += x
    
        def readName(self):
            return self[0]
    
        def changeName(self, newName):
            self[0] = newName
    
        def readSize(self):
            return self[1]
    
        def changeSize(self, newSize):
            self[1] = newSize
    
        def readItem(self):
            return self
    
    class Inventory(list):
        def addEntry(self, item):
            self.append(item)
    
        def readEntry(self, item):
            i = 0
            while i < len(self):
                if item == self[i]:
                    return item.readItem()
                else:
                    i += 1
    
        def readAll(self):
            i = 0
            while i < len(self):
                self[i].readItem()
                i +=1
    The readAll function of the Inventory class will not return anything though. The weird part that is throwing me for a loop though is that if I put the code into the shell it runs perfect. I switch the instances of, 'self,' out for whatever variable I assigned the instance of Inventory to, and it returns every entry no issue. I'm not sure why it is doing this, and would be very thankful if someone could explain it to me, and hopefully shed light on what I can do to fix it.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    33
    Rep Power
    3
    i can't test the code from where i am now but the main problem i see is ur making a call to ’self’ as if it is a parameter... u don't do this, instead do this ’self.list_of_items’ it doesn't have to be list_of_items i can be anything but not self on its own hope this helps u fix your code


    Edit:
    forgot to mention, make your variables in ur __init__ function so you can make your inventory list there i.e. self.inventory = [name, size]
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    @purity_lake
    1)Purity Ice Cream, Ithaca, NY, USA makes a wonderful product.
    2)Fetlock19's classes are subclasses of list. Hence they inherit all the methods and class variables of list. Hence Fetlock19 certainly can write as did.
    >>> class c(list):''
    ...
    >>> c((1,2))
    [1, 2]
    >>> c((1,2))[1]
    2
    >>>

    @Fetlock19
    Code:
    '''
        A dictionary type is probably more appropriate
        Or python3 properties would also be useful.
    
        dictionary example:
    
        >>> inventory = {'shirts':3,'ties':666,'slacks':8}
        >>> inventory['shirts']
        3
        >>> print(list(sorted(inventory.keys())))
        ['shirts', 'slacks', 'ties']
    '''
    
    class Item(list):
    
        def __init__(self, name = "Default", size = 1):
            ################ simplified ################
            super(Item, self).__init__([name,size])
    
        def readName(self):
            return self[0]
    
        def changeName(self, newName):
            self[0] = newName
    
        def readSize(self):
            return self[1]
    
        def changeSize(self, newSize):
            self[1] = newSize
    
        def readItem(self):
            return self
    
    class Inventory(list):
    
        '''
            >>> i=Inventory([Item('a',4)])  # correct Inventory construction
            >>> i.readAll()
            [['a', 4]]
            >>> Inventory(Item('mistake!',666)) # list constructor does not require items
            ['mistake!', 666]
            >>> 
        '''
    
        #this constructor is safer if you also fix addEntry
        #def __init__(self,iterable):
        #    super(self).__init__()
        #    for item in iterable:
        #        self.addEntry(item)
    
        def addEntry(self, item):
            #assert isinstance(item,Item)
            self.append(item)
        # since you haven't bothered to test isintance(item,Item)
        # you may as well write
        #addEntry = list.append
    
        def readEntry(self, item):
            for o in self:
                if item == o:
                    return o
    
        #Why don't you just use
        #readEntry = list.index
        #>>> help([].index)
    
        ##look up by name, rather than by name and size looks more useful to me
        #
        #def readEntry(self, NAME):
        #    for o in self:
        #        if item.readName() == NAME:
        #            return item
        #    return Item(name=NAME,size=0)
    
        def readAll(self):
            return self[:] # return a shallow copy of the inventory
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    33
    Rep Power
    3
    oh ok, i understand thanks for pointing that out
    also I've never heard of that ice cream company, i must peruse their products

    Comments on this post

    • b49P23TIvg agrees : http://www.purityicecream.com/
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    Thank you very much for your input, it has been very helpful. As I said, I'm still really new. Started learning maybe a week ago now. Ripped through one book and just started working with a little bit more robust one.

IMN logo majestic logo threadwatch logo seochat tools logo