DEV Community

Discussion on: A GENTLE INTRODUCTION TO PYTHON CLASSES FOR NEWBIES

Collapse
 
sab6 profile image
Shereef Bankole

Many thanks for your time and the great effort in writing a step by step guide on how to write classes and build test cases in python. One of the beauties of object-oriented programming is that real-world things can be modeled with it. As many of the readers of the articles, I have one of two takeaways from the article. However, some of my comments are highlighted below.

  1. Just for emphasis, newbies should be aware that by convention, each first letter in which a class is composed of is capitalized (for some reasons) e.g. Cake and Pizza and OrderPizza (note that Order and Pizza are two separate words, but because you can’t have space in between the two in python, both were combined into OrderPizza). It is not advisable to use underscore (_) in naming a class.

  2. The dash before and after the _init_() method are four underscores i.e. two before and two after init word.

  3. The class Cake was used to introduce how to create classes, but in the description of the _init_() method Pizza class which came latter was referred to (I guess it’s an oversight).

  4. In the test case, make_marinara() method was tested, I know it would be part of the module imported during the testing; however, it wasn’t shown as part of the methods associated with the class Pizza. Some that are following the codes won't be aware of this.

  5. The order_margherita method associated with the Pizza class is not modifying any attributes. If an initial value had been set for drink in the _init_ () method, one can modify or update the value with a method or directly.

I think the code might look like this:

Class Pizza:

def _init_(self, veggies, toppings, crust, oil):

self.veggies = veggies

self.toppings = veggies

self.crust = crust

self.oil = oil

self.drink = 'fanta' #attribute with a default value needn’t be included as part of the #parameters in the _init_() method

def order_margherita(self):

return f”…

def modify_drink(self):

self.drink = ''

your_order =Pizza(veggies, toppings, crust, oil) #create an instance your_order

your_order = your_order.modify_drink(‘coke)

print(your_order. order_margherita())

Collapse
 
oluchi profile image
Oluchi Orji

Good catch!I will update the article pretty soon!