# Order.py # Class Method page 57 # Filename: Order.py # Class name: Order # Constructor name: Order() # Tuples() construct immutable data # Best practice instance name: order # Garbage Collection Finalizer name: ~Order # 'Factory Methods' initialize on a class, using VERY VERY specific parameters that commonly ARE NOT a part of the constructor 'extends attrs on the class' class Order: # Classes Attributes tax_rate = 0 # class-wide attr for tax_rate, default value is .05 vat_rate = 1.10 # vat tax # Constructors Attributes Order() def __init__(self, orderid, amount): self.orderid = orderid self.amount = amount # define a method of setting the tax rate @classmethod def set_taxRate(cls, new_tax_rate): cls.tax_rate = new_tax_rate # set the new value if passed ACROSS the WHOLE Class, INCLUDING all instance objects of the class __init__() or static,instance or even other class methods # instance method to calc total def calculate_total(self): return self.amount + (self.amount * Order.tax_rate) # Output order1 = Order("1001", 1000) # Object 1 we are going to use the default tax rate of 0 print(f'Amount: ${order1.amount} Total: ${order1.calculate_total():.2f}') # Amount: 1000 Total: 1000 # Object 2 Set and New tax rate then calculation Order.set_taxRate(.05) order2 = Order("1002", 1000) print(f'Amount: ${order2.amount} Total: ${order2.calculate_total():.2f}')