class Dates:
def __init__(self, date):
self.date = date
def getDate(self):
return self.date
@staticmethod
def toDashDate(date):
return date.replace("/", "-")
date = Dates("15-12-2016")
dateFromDB = "15/12/2016"
dateWithDash = Dates.toDashDate(dateFromDB)
if(date.getDate() == dateWithDash):
print("Equal")
else:
print("Unequal")
@classmethod | @staticmethod |
---|
Declares a class method. | Declares a static method. |
It can access class attributes, but not the instance attributes. | It cannot access either class attributes or instance attributes. |
It can be called using the ClassName.MethodName() or object.MethodName() . | It can be called using the ClassName.MethodName() or object.MethodName() . |
It can be used to declare a factory method that returns objects of the class. | It cannot return an object of the class.
|
I encourage you to use staticmethod since it makes a less confusing.