Python Forum
I'm trying to make a constructor without hardcoding all of the values
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm trying to make a constructor without hardcoding all of the values
#3
Here is a working version
#!/usr/bin/env python3
#PublicPizzaClass.py
 
class Pizza:
    #a constructor that initializes 3 attributes for any-topping selfs.
    #right now, the attributes are public, so code outside of this class
    #will be able to get and set the values of these attributes.
    def __init__(self, name, size, price=None, taxPercent=0.05):
        self.name = name
        self.size = size #small = $7, medium = $9, large = $10
        self.taxPercent = taxPercent
        self.price = self.getSizePrice(size) if price is None else price
        
    #a method that associates a size with a price
    @staticmethod
    def getSizePrice(size):
        if size == "Small":
            price = 7.00
        elif size == "Medium":
            price = 9.00
        elif size == "Large":
            price = 10.00
        return price
 
    #a method that uses two attributes to perform a calculation
    def getTaxAmount(self):
        return self.price * self.taxPercent
 
    #a method that calls another method to perform a calculation
    def getTotalPrice(self):
        return self.price + self.getTaxAmount()
    
 
#create two pizza objects
pizza1 = Pizza("Pepperoni", "Large")
pizza2 = Pizza("Sausage", "Medium")
 
print("Name:            {:s}".format(pizza1.name))
print("Price:           {:.2f}".format(pizza1.price))
print("Tax Amount:      {:.2f}".format(pizza1.getTaxAmount()))
print("Total Price:     {:.2f}".format(pizza1.getTotalPrice()))
Note that the implicit first argument in methods is usually named 'self' instead of 'pizza'. In this version, the constructor gives you the opportunity to give a price and tax percent, but if you don't give it, it chooses the default (price from size and tax 0.05).
Reply


Messages In This Thread
RE: I'm trying to make a constructor without hardcoding all of the values - by Gribouillis - Apr-05-2018, 05:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 2,120 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Simple Python API, need packaging/removing hardcoding zxcv101 2 2,091 Jun-13-2022, 03:09 PM
Last Post: zxcv101
  Not including a constructor __init__ in the class definition... bytecrunch 3 19,766 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  How to make a list of values from a dictionary list? faryad13 2 2,899 Sep-03-2020, 03:45 PM
Last Post: faryad13
  syntaxerror when entering a constructor MaartenRo 2 2,855 Aug-03-2020, 02:09 PM
Last Post: MaartenRo
  error in constructor overriding in python3 srm 1 2,652 Jul-18-2019, 12:21 PM
Last Post: ichabod801
  This constructor takes no arguments Friend 2 7,464 Jun-26-2019, 02:54 PM
Last Post: Friend
  class constructor with dataframe UGuntupalli 2 3,235 Jun-11-2019, 10:50 PM
Last Post: UGuntupalli
  Overload of constructor psosmol 2 3,715 Apr-17-2019, 05:10 AM
Last Post: psosmol
  How to Make Python code execution pause and resume to create .csv and read values. Kashi 2 4,772 Jun-14-2018, 04:16 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020