[Class]: can't use the .key and .items method to call these from a disctionary creted inside a class object?


i'm doing the lesson "It's Not All Animals and Fruits" and if i have to admit i've not understood too well how classes work the lesson was passed easily with this code:

class ShoppingCart(object):
    """Creates shopping cart objects
    for users of our fine website."""
    items_in_cart = {}
    def __init__(self, customer_name):
        self.customer_name = customer_name

    def add_item(self, product, price):
        """Add product to the cart."""
        if not product in self.items_in_cart:
            self.items_in_cart[product] = price
            print product + " added."
            print product + " is already in the cart."

    def remove_item(self, product):
        """Remove product from the cart."""
        if product in self.items_in_cart:
            del self.items_in_cart[product]
            print product + " removed."
            print product + " is not in the cart."

my_cart = ShoppingCart("Alex")
my_cart.add_item("macine", 0.88)
my_cart.add_item("cornetto", 1.05)
my_cart.add_item("pistacchi", 1.99)

but since i want to see in the interpreter what i've done i've tried to add a print statement at the end to show on a single row the item added to shopping cart and its price. so i thought to review the .key and .items methods with:

print my_cart.items_in_cart.keys
print my_cart.items_in_cart.items

but it returns:
'built-in method keys of dict object at 0x1aeee60'
'built-in method items of dict object at 0x1b08cc0'

my thoughts were:

my_cart is object where i need ot pull out the dictionary
my.cart.items_in _cart is the dictionary indeed (print my_cart.items_in_cart confirmed this)
my.cart.items_in _cart.items should call the item list from that dictionary

i've achieved the result with a simple loop...

for key in my_cart.items_in_cart:
    print key, my_cart.items_in_cart[key]

but i want to know... where is my logic broken?


but calling methods requires parentheses:

print my_cart.items_in_cart.keys()

otherwise, python just tells you its a method (as you have seen)



what a noob



do you understand it better now or do you have more questions?


think i'm fine now..... 'till next trouble :smiley:

thx for fast reply!


OFF-TOPIC question:

is there a chat to get direct help for questions that aren't related to exercizes or that isn't worth keep on forum?

like for example: why do we ever need class inheritance at all? just make a single class with all methods needed

(sorry if i keep asking stupid things)


pro users have a chat in which they can ask advisers for help

that is actually a really good question, you could make a new topic for that

Well, if you actually design a GUI program, having a single class for all frames is really difficult. Or in my django app, if all pages where rendered by the same class, it would become a mess, and really difficult to tell which url should load what content

Multiply classes keep your code better organized and more flexible

Its difficult to explain, but if you get more into OOP it will become more clear

If you need to write modulair code, having a single class doesn't make your code very modulair


yes i understand modularity and i love it... just it makes a pain to read the code, following the calls up to the "source"

but probably it's just 'cause i'm new and not yet really on the "piece"

i'm not even close to make GUI stuff so better forgive the question for later :wink:

thx again Master!

P.S.: clarify: "with following the calls up to the "source"" i mean that i can figure a class calling other class.... that calls other class again and so on :S


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.