[Tutor] Using Class Properly - early beginner question

boB Stepp robertvstepp at gmail.com
Fri Mar 24 17:42:14 EDT 2017


I'm forwarding this to Tutor.  Please respond to the whole group and
not just me personally, so you can have access to the experts as well
as allowing all of us learners the opportunity to learn more.

On Fri, Mar 24, 2017 at 9:05 AM, Rafael Knuth <rafael.knuth at gmail.com> wrote:
> I have another question :)
> I noticed that you split your class into three methods:
>
> def __init__(self):
> # initialize instances of class
>
> def make_shopping_list(self):
> # input
>
> def display_shopping_list(self):
> # output
>
> I was wondering what common practices are to structure a class?
> Thanks :)
>
> class GroceryListMaker:
>     def __init__(self):
>         self.shopping_list = []
>         self.prompt = ("what food items would you like to buy?\nType
> 'quit to exit: ")
>
>     def make_shopping_list(self):
>         while True:
>             food = input(self.prompt)
>             if food == "quit":
>                 break
>             else:
>                 self.shopping_list.append(food)
>
>     def display_shopping_list(self):
>         print("\n\nYour shopping list now has the following items:\n")
>         for item_number, item in enumerate(self.shopping_list):
>             print("%s. %s" % (item_number, item))
>
> if __name__ == "__main__":
>     my_shopping_list = GroceryListMaker()
>     my_shopping_list.make_shopping_list()
>     my_shopping_list.display_shopping_list()



-- 
boB


More information about the Tutor mailing list