3

I need to write a "store". I have class ClothingItem -- Shoe, Pant and Shirt all inherit from ClothingItem. Now I'm working on class Inventory(list). I can add shoes and pants and shirts, and get a total count (inventory.len()). Now Dad wants me to tell him how many shoes are in inventory, how many pants, whatever. He says that if my store starts selling Shorts or Leggings, I'm not allowed to touch my Inventory class. He said something about passing a TYPE to the length function, but I can't figure out how to ask Google that. He works in C# so he only knows how to do it in that, not in Python.

So... can someone PLEASE tell me where to look to figure this out? He said if you post code he'll know and just make me do something harder, so please don't send me code! Just tell me where I should look and I'll be super-awesome grateful.

5
  • 1
    Welcome to Stack Overflow; best of luck to you. If you post your code here, it'll be far easier for others to give you feedback. Since we're generally loathe to Just Post The Code when asked, you probably won't have much trouble about people just posting fixed code. :) Commented Jan 5, 2012 at 23:20
  • This could fit better on programmers.SE (if it fits somewhere at all). Commented Jan 5, 2012 at 23:23
  • 6
    It not be a very good question, but it doesn't deserve -3. Jacqueline, if you don't like programming, stop learning it. If it's for fun, it's apparently not a success. If it's for work, programming is one of these jobs where enjoying the process of solving a problem has a huge influence of the quality of you professional life, including pleasure, money, competence and many more. You are going to be happier with a subject that doesn't "kill you", and there is nothing wrong with that. Commented Jan 5, 2012 at 23:27
  • 1
    Just out of curiosity, why exactly does your dad want you to learn programming? It seems counterproductive at this point to me. Commented Jan 6, 2012 at 0:09
  • 1
    Might also be suitable for parenting.SE :( Commented Jan 6, 2012 at 0:22

3 Answers 3

3

If your class ClothingItem includes a method like: in_stock() that returns a string built from self.name and self.quantity, every subclass (Shoe, Pant, Shirt, Legging, etc.) just needs to assign self.name correctly when it is instantiated.

What this does is provide something called polymorphism (though perhaps the Duck Typing wikipedia article is a better introduction. Skip bits you don't understand.) so that subclasses all behave differently -- but in a manner that "fits" with the names you give them.

Incidentally, if this were my problem to solve, I probably wouldn't have subclassed ClothingItem for every type of clothing in the store -- that means you need to edit code in order to carry a new product. This design is good enough for learning how to work with subclassing, but this isn't an ideal use. (Instead, I'd just store the clothing type as a member of the Clothing class. Pants, shirts, shoes, gorillas, you treat them all the same -- a name, how many you have, how much you sell them for, how much you pay for them from your supplier, etc.)

Sign up to request clarification or add additional context in comments.

4 Comments

thanks you i'll read the article. However, my dad will probably say that that makes the class ClothingItem to smart because it has to know about the inventory
You wouldn't need to modify ClothingItem each time you add a new piece of clothing -- but you would need to write a new Legging or Short class or whatever each time you add a new product.
Man, I like teaching beginners how to program, but this is really sad. :( I'm sorry Jacqueline, programming ought to be fun.
Unless you're going to generate classes dynamically (known as meta-programming, which I'd say is an advanced topic) then @sarnold is right, it's better to make the type of clothing an attribute. Don't worry about classes being "too smart", as long as they produce what the human wants then they're just smart enough.
3

There's a few ways you can tackle this problem. The first one that pops to mind is to use the isinstance and filter functions (or a list comprehension in place of filter... but if you're just learning, comprehensions might be too much too soon) to pare down your list into just items that belong to a specific class.

Also, remind your dad that if learning programming is "killing you", he's created the wrong environment for you to learn. No one thrives in a topic when they feel that way. As someone who loves programming, it really makes me sad to see that happen to people.

1 Comment

THANK YOU for agreeing with me for the past few years i haven't had any social life, when im with him because he says "you have to get your programming done before you go out". I never get to hang out with my friends anymore and I always miss out on fun activities. Now my friends have stoped inviting me to as many things that i used to be invited to. it makes me really sad :(
1

isinstance()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.