Mar-25-2020, 04:43 PM
Hi there, in the following code I have a class method called addFormattedCosts. I get that when working with instance variables you use the "self" keyword according to convention. In the below function I didn't use the self before the variables within the method and didn't get an error. The variables used in the method are like "helper" variables and nothing interacts with any actual instance variables which Is why I think I don't get errors. But what affect will occur by not putting the ".self" before these "helper" variables. Should I and why?
1 2 3 4 5 |
def addFormattedCosts( self , costList): millionFormat = ( float (cost.replace( 'M' , '')) * 1000000 if "M" in cost else 0.0 for cost in costList) thousandFormat = ( float (cost.replace( 'K' , '')) * 1000 if "K" in cost else 0.0 for cost in costList) finalSum = sum (millionFormat) + sum (thousandFormat) return int (finalSum) |