[Tutor] Using Class Properly - early beginner question

boB Stepp robertvstepp at gmail.com
Tue Mar 21 22:48:49 EDT 2017

On Tue, Mar 21, 2017 at 12:20 PM, Rafael Knuth <rafael.knuth at gmail.com> wrote:

> While writing the parent class, I ran into the following issue:
> How do I properly declare a variable that takes user input?
> Do I write methods in the same fashion like in a regular function?
> And how do I call that class properly?

When you "call the class" as you put it, you are asking the class to
create a specific, new instance of that class, a new object.  You may
or may not have to pass one or more arguments into the __init__ method
of that class.  How do you know?  What parameters (other than "self")
does the __init__ method have?  It needs those to create an object
instance.  What is required depends on how you set up your class'
__init__ method.  Once you have created an object from a class, then
you may access attributes or methods defined for that object using
"dot" notation.  Methods are very much like functions with the
exception that they are tied to an object, thus the "self" parameter
which refers to that object instance.

> This is what I came up with:
> class BuyFoods(object):
>     def __init__(self, outlet):
>         self.outlet = outlet
>     def CreateShoppingList(self, shopping_list, prompt, food):
>         self.shopping_list = shopping_list
>         self.prompt = prompt
>         self.food = food
>         shopping_list = []
>         prompt = ("Which foods would you like to purchase?\nEnter
> 'quit' to exit. ")
>         food = input(prompt)
>         while food != "quit":
>             shopping_list.append(food)
>             food = input(prompt)
>         print("You just purchased these foods: %s." % ", ".join(shopping_list))
> Tesco = BuyFoods("Tesco")

Here is where you created your new object, "Tesco".  Now you can
access *this* object's methods and attributes with the dot notation.

> Tesco.CreateShoppingList()

And here you try to access the "CreateShoppingList" method of the
object "Tesco".  [Note:  It is customary to use the same naming
conventions for methods that you use for functions.  So instead of
using camel case for the method's name, it would be more customary to
use a name format, "create_shopping_list".]  But notice in your class
definition that this method requires the following arguments to be
passed into it:  self, shopping_list, prompt, and food.  You provided
"self" automatically by creating the object, "Tesco", and accessing
its "CreateShoppingList()" method, but you did not provide the other
arguments.  Thus the error message you get below.  You would need to
call it something like:

Tesco.CreateShoppingList(['milk', 'bread', 'oranges'], 'Which foods
would you like to purchase?\nEnter "quit" to exit.', 'spam')

This would clear the immediate errors, but the logic of your method is
not designed to make use of these arguments being passed in.  Instead,
you do nothing with them and basically ask for all of this information
again.  Does this make sense to you?

> That's the error message I get:
> Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900
> 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license()" for more information.
> == RESTART: C:\Users\Rafael\Documents\01 - BIZ\PYTHON\Python Code\PPC_28.py ==
> Traceback (most recent call last):
>   File "C:\Users\Rafael\Documents\01 - BIZ\PYTHON\Python
> Code\PPC_28.py", line 136, in <module>
>     Tesco.CreateShoppingList()
> TypeError: CreateShoppingList() missing 3 required positional
> arguments: 'shopping_list', 'prompt', and 'food'

So in your method, to do what it looks like you are trying to do *with
arguments*, you should be doing things like:

food = input(self.prompt)



But if you truly want the user to be inputting this information, then
you don't want your method to have these arguments!  So you have to
choose which approach you want to do.



More information about the Tutor mailing list